Posts

Showing posts with the label JavaScript best practice

Extremely Useful Hacks for JavaScript

Image
In this post I will share  12 extremely useful hacks for JavaScript . These hacks reduce the code and will help you to run optimized code. So let’s start hacking! 1) Converting to boolean using  !!  operator Sometimes we need to check if some variable exists or if it has a valid value, to consider them as  true value . For do this kind of validation, you can use the  !! (Double negation operator)  a simple  !!variable , which will automatically convert any kind of data to boolean and this variable will return  false  only if it has some of these values:  0 ,  null ,  "" ,  undefined  or  NaN , otherwise it will return  true . To understand it in practice, take a look this simple example: function Account ( cash ) { this .cash = cash; this .hasMoney = !!cash; } var account = new Account( 100.50 ); console .log(account.cash); // 100.50 console .log(account.hasMoney); // true ...

Three JavaScript Array Hacks

Image
Arrays are everywhere in JavaScript and with the new  spread operators introduced in ECMAScript 6, you can do awesome things with them. In this post I will show you three useful tricks you can use when programming: 1. Iterating through an empty array JavaScript arrays are sparse in nature in that there are a lot of holes in them. Try creating an array using the Array’s constructor and you will see what I mean. > const arr = new Array(4); [undefined, undefined, undefined, undefined] You may find that iterating over a sparse array to apply a certain transformation is hard. > const arr = new Array(4); > arr.map((elem, index) => index); [undefined, undefined, undefined, undefined] To solve this, you can use  Array.apply  when creating the array. > const arr = Array.apply(null, new Array(4)); > arr.map((elem, index) => index); [0, 1, 2, 3] A nicer way to iterate through an empty array is to use spread operators. Shout-out to  ...

Javascript Hacks and Tips To Optimize Performance and Save time

Image
JavaScript or JS helps implement complex things on web pages. Many of the developers know the importance of an minified Javascript file but few are aware of an Optimized Javascript code. An optimized code is a combination of smartly programmed logics and small hacks to optimize performance, speed and save time. Here are sweet 16  JS hacks and tips  for developers  for optimizing Javascript to improve JS performance and improve execution time without affecting server resources. 1. Use Array Filter It is a small hack to filter out bucket of elements from the array pool.  This method creates an array filled with all array elements that pass a test (provided as a function).  According to requirement create a callback function for non-required elements. In below example the bucket elements are  null  and are ready to get filtered out.    Example: schema = [ "hi" , "ihaveboyfriend" , null , null , "goodbye"] schema = schema ....

Popular posts from this blog

How to download a file using command prompt (cmd) Windows?

The future of Artificial Intelligence: 6 ways it will impact everyday life

Angular 9 - User Registration and Login Example & Tutorial