<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Shajib1312</title>
    <description>The latest articles on DEV Community by Shajib1312 (@shajib1312).</description>
    <link>https://dev.to/shajib1312</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1098410%2F809259e6-7474-4614-a0f1-af500aa2cf1a.jpeg</url>
      <title>DEV Community: Shajib1312</title>
      <link>https://dev.to/shajib1312</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/shajib1312"/>
    <language>en</language>
    <item>
      <title>JavaScript Tips: Useful Tricks and Best Practices</title>
      <dc:creator>Shajib1312</dc:creator>
      <pubDate>Sat, 20 Apr 2024 03:03:59 +0000</pubDate>
      <link>https://dev.to/shajib1312/javascript-tips-useful-tricks-and-best-practices-4cb7</link>
      <guid>https://dev.to/shajib1312/javascript-tips-useful-tricks-and-best-practices-4cb7</guid>
      <description>&lt;p&gt;As you probably are aware, JavaScript is the main programming language on the planet, the language of the web, of versatile mixture applications (like PhoneGap or Appcelerator), of the server side (like NodeJS or Wakanda) and has numerous different executions. It's likewise the beginning stage for the overwhelming majority new designers to the universe of programming, as it very well may be utilized to show a straightforward caution in the internet browser yet in addition to control a robot (utilizing nodebot, or nodruino). The designers who ace JavaScript and compose coordinated and performant code have turned into the most pursued in the gig market.&lt;/p&gt;

&lt;p&gt;In this article, I'll share a bunch of JavaScript tips, deceives and best practices that ought to be known by all JavaScript designers no matter what their program/motor or the SSJS (Waiter Side JavaScript) translator.&lt;/p&gt;

&lt;p&gt;The V8 JavaScript Engine (V8 3.20.17.15) is used in the most recent version of Google Chrome (30), which is where the code samples in this article have been tested.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1&lt;/strong&gt; – Don’t forget &lt;strong&gt;var&lt;/strong&gt; keyword when assigning a variable’s value for the first time.&lt;/p&gt;

&lt;p&gt;Assignment to an undeclared variable automatically results in a global variable being created. Avoid global variables.&lt;/p&gt;

&lt;p&gt;2 &lt;strong&gt;– use === instead of ==&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;== (or !=)&lt;/strong&gt; operator performs an automatic type conversion if needed. The &lt;strong&gt;=== (or !==) **operator will not perform any conversion. It compares the **value **and the **type&lt;/strong&gt;, which could be considered faster than &lt;strong&gt;==&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[10] === 10    // is false
[10]  == 10    // is true
'10' == 10     // is true
'10' === 10    // is false
 []   == 0     // is true
 [] ===  0     // is false
 '' == false   // is true but true == "a" is false
 '' ===   false // is false 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;3 – undefined, null, 0, false, NaN, '' (empty string) are all falsy.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4 – Use Semicolons for line termination&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The use of semi-colons for line termination is a good practice. You won’t be warned if you forget it, because in most cases it will be inserted by the JavaScript parser. For more details about why you should use semi-colons, take a look to this artice: &lt;a href="https://davidwalsh.name/javascript-semicolons"&gt;https://davidwalsh.name/javascript-semicolons&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5 – Create an object constructor&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function Person(firstName, lastName){
    this.firstName =  firstName;
    this.lastName = lastName;        
}  

var Saad = new Person("Saad", "Mousliki");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;6 – Be careful when using &lt;strong&gt;typeof&lt;/strong&gt;, &lt;strong&gt;instanceof **and **constructor&lt;/strong&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;typeof&lt;/strong&gt; : a JavaScript unary operator used to  return a string that represents the primitive type of a variable,  don’t forget that typeof null will return “object”, and for the majority of object types (Array, Date, and others) will return also “object”.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;constructor&lt;/strong&gt; : is a property of the internal prototype property, which could be overridden by code.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;*&lt;em&gt;instanceof *&lt;/em&gt;: is another JavaScript operator that check in all the prototypes chain the constructor it returns true if it’s found and false if not.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var arr = ["a", "b", "c"];
typeof arr;   // return "object" 
arr  instanceof Array // true
arr.constructor();  //[]

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;7 – Create a Self-calling Function&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(function(){
    // some private code that will be executed automatically
})();  
(function(a,b){
    var result = a+b;
    return result;
})(10,20)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is often called a Self-Invoked Anonymous Function or Immediately Invoked Function Expression (IIFE). It is a function that executes automatically when you create it, and has the following form:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;8 – Get a random item from an array&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var items = [12, 548 , 'a' , 2 , 5478 , 'foo' , 8852, , 'Doe' , 2145 , 119];

var  randomItem = items[Math.floor(Math.random() * items.length)];

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;9 – Get a random number in a specific range&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This code snippet can be useful when trying to generate fake data for testing purposes, such as a salary between min and max.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;var x = Math.floor(Math.random() * (max - min + 1)) + min;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;10 – Generate an array of numbers with numbers from 0 to max&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var numbersArray = [] , max = 100;

for( var i=1; numbersArray.push(i++) &amp;lt; max;);  // numbers = [1,2,3 ... 100] 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;11 – Generate a random set of alphanumeric characters&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function generateRandomAlphaNum(len) {
    var rdmString = "";
    for( ; rdmString.length &amp;lt; len; rdmString  += Math.random().toString(36).substr(2));
    return  rdmString.substr(0, len);

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;12 – Shuffle an array of numbers&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var numbers = [5, 458 , 120 , -215 , 228 , 400 , 122205, -85411];
numbers = numbers.sort(function(){ return Math.random() - 0.5});
/* the array numbers will be equal for example to [120, 5, 228, -215, 400, 458, -85411, 122205]  */
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A better option could be to implement a random sort order by code (e.g. : Fisher-Yates shuffle), than using the native sort JavaScript function. For more details take a look to this discussion.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;13 – A string trim function&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The classic trim function of Java, C#, PHP and many other language that remove whitespace from a string doesn’t exist in JavaScript, so we could add it to the **String **object.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;String.prototype.trim = function(){return this.replace(/^s+|s+$/g, "");};&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;A native implementation of the &lt;strong&gt;trim()&lt;/strong&gt; function is available in the recent JavaScript engines.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;14 – Append an array to another array&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var array1 = [12 , "foo" , {name "Joe"} , -2458];

var array2 = ["Doe" , 555 , 100];
Array.prototype.push.apply(array1, array2);
/* array1 will be equal to  [12 , "foo" , {name "Joe"} , -2458 , "Doe" , 555 , 100] */
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;15 – Transform the arguments object into an array&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;var argArray = Array.prototype.slice.call(arguments);&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;16 – Verify that a given argument is a number&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function isNumber(n){
    return !isNaN(parseFloat(n)) &amp;amp;&amp;amp; isFinite(n);
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;17 – Verify that a given argument is an array&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function isArray(obj){
    return Object.prototype.toString.call(obj) === '[object Array]' ;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that if the &lt;strong&gt;toString()&lt;/strong&gt; method is overridden, you will not get the expected result using this trick.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Or use…&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Array.isArray(obj); // its a new Array method&lt;/code&gt;&lt;br&gt;
You could also use **instanceof **if you are not working with multiple frames. However, if you have many contexts, you will get a wrong result.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var myFrame = document.createElement('iframe');
document.body.appendChild(myFrame);

var myArray = window.frames[window.frames.length-1].Array;
var arr = new myArray(a,b,10); // [a,b,10]  

// instanceof will not work correctly, myArray loses his constructor 
// constructor is not shared between frames
arr instanceof Array; // false
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;18 – Get the max or the min in an array of numbers&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var  numbers = [5, 458 , 120 , -215 , 228 , 400 , 122205, -85411]; 
var maxInNumbers = Math.max.apply(Math, numbers); 
var minInNumbers = Math.min.apply(Math, numbers);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;19 – Empty an array&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var myArray = [12 , 222 , 1000 ];  
myArray.length = 0; // myArray will be equal to [].
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;20 – Don’t use delete to remove an item from array&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Use **splice **instead of using **delete **to delete an item from an array. Using delete replaces the item with undefined instead of the removing it from the array.&lt;/p&gt;

&lt;p&gt;Instead of…&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var items = [12, 548 ,'a' , 2 , 5478 , 'foo' , 8852, , 'Doe' ,2154 , 119 ]; 
items.length; // return 11 
delete items[3]; // return true 
items.length; // return 11 
/* items will be equal to [12, 548, "a", undefined × 1, 5478, "foo", 8852, undefined × 1, "Doe", 2154,       119]   */
Use…

var items = [12, 548 ,'a' , 2 , 5478 , 'foo' , 8852, , 'Doe' ,2154 , 119 ]; 
items.length; // return 11 
items.splice(3,1) ; 
items.length; // return 10 
/* items will be equal to [12, 548, "a", 5478, "foo", 8852, undefined × 1, "Doe", 2154,       119]   */
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The delete method should be used to delete an object property.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;21 – Truncate an array using length&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Like the previous example of emptying an &lt;strong&gt;array&lt;/strong&gt;, we truncate it using the **length **property.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var myArray = [12 , 222 , 1000 , 124 , 98 , 10 ];  
myArray.length = 4; // myArray will be equal to [12 , 222 , 1000 , 124].
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As a bonus, if you set the array length to a higher value, the length will be changed and new items will be added with undefined as a value. The array length is not a read only property.&lt;/p&gt;

&lt;p&gt;myArray.length = 10; // the new array length is 10 &lt;br&gt;
myArray[myArray.length - 1] ; // undefined&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;22 – Use logical AND/ OR for conditions&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var foo = 10;  
foo == 10 &amp;amp;&amp;amp; doSomething(); // is the same thing as if (foo == 10) doSomething(); 
foo == 5 || doSomething(); // is the same thing as if (foo != 5) doSomething();
The logical OR could also be used to set a default value for function argument.

function doSomething(arg1){ 
    arg1 = arg1 || 10; // arg1 will have 10 as a default value if it’s not already set
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;23 – Use the map() function method to loop through an array’s items&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var squares = [1,2,3,4].map(function (val) {  
    return val * val;  
}); 
// squares will be equal to [1, 4, 9, 16] 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;24 – Rounding number to N decimal place&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var num =2.443242342;
num = num.toFixed(4);  // num will be equal to 2.4432
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;NOTE : the **toFixed() **function returns a string and not a number.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;25 – Floating point problems&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0.1 + 0.2 === 0.3 // is false 
9007199254740992 + 1 // is equal to 9007199254740992  
9007199254740992 + 2 // is equal to 9007199254740994
Why does this happen? 0.1 +0.2 is equal to 0.30000000000000004. What you need to know is that all JavaScript numbers are floating points represented internally in 64 bit binary according to the IEEE 754 standard. For more explanation, take a look to this blog post.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can use &lt;strong&gt;toFixed()&lt;/strong&gt; and &lt;strong&gt;toPrecision()&lt;/strong&gt; to resolve this problem.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;26 – Check the properties of an object when using a for-in loop&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This code snippet could be useful in order to avoid iterating through the properties from the object’s prototype.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for (var name in object) {  
    if (object.hasOwnProperty(name)) { 
        // do something with name                    
    }  
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;27 – Comma operator&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var a = 0; 
var b = ( a++, 99 ); 
console.log(a);  // a will be equal to 1 
console.log(b);  // b is equal to 99
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;28 – Cache variables that need calculation or querying&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In the case of a jQuery selector, we could cache the DOM element.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var navright = document.querySelector('#right'); 
var navleft = document.querySelector('#left'); 
var navup = document.querySelector('#up'); 
var navdown = document.querySelector('#down');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;29 – Verify the argument before passing it to isFinite()&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;isFinite(0/0) ; // false 
isFinite("foo"); // false 
isFinite("10"); // true 
isFinite(10);   // true 
isFinite(undefined);  // false 
isFinite();   // false 
isFinite(null);  // true  !!! 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;30 – Avoid negative indexes in arrays&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var numbersArray = [1,2,3,4,5]; 
var from = numbersArray.indexOf("foo") ;  // from is equal to -1 
numbersArray.splice(from,2);    // will return [5]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Make sure that the arguments passed to **splice **are not negative.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;31 – Serialization and deserialization (working with JSON)&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var person = {name :'Saad', age : 26, department : {ID : 15, name : "R&amp;amp;D"} }; 
var stringFromPerson = JSON.stringify(person); 
/* stringFromPerson is equal to "{"name":"Saad","age":26,"department":{"ID":15,"name":"R&amp;amp;D"}}"   */ 
var personFromString = JSON.parse(stringFromPerson);  
/* personFromString is equal to person object  */
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;32 – Avoid the use of eval() or the Function constructor&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Use of eval or the Function constructor are expensive operations as each time they are called script engine must convert source code to executable code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var func1 = new Function(functionCode);
var func2 = eval(functionCode);

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;33 – Avoid using with() (The good part)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Using &lt;strong&gt;with()&lt;/strong&gt; inserts a variable at the global scope. Thus, if another variable has the same name it could cause confusion and overwrite the value.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;34 – Avoid using for-in loop for arrays&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Instead of using…&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var sum = 0;  
for (var i in arrayNumbers) {  
    sum += arrayNumbers[i];  
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;…it’s better to use…&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var sum = 0;  
for (var i = 0, len = arrayNumbers.length; i &amp;lt; len; i++) {  
    sum += arrayNumbers[i];  
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As a bonus, the instantiation of i and len is executed once because it’s in the first statement of the for loop. Thsi is faster than using…&lt;/p&gt;

&lt;p&gt;&lt;code&gt;for (var i = 0; i &amp;lt; arrayNumbers.length; i++)&lt;/code&gt;&lt;br&gt;
Why? The length of the array arrayNumbers is recalculated every time the loop iterates.&lt;/p&gt;

&lt;p&gt;NOTE : the issue of recalculating the length in each iteration was fixed in the latest JavaScript engines.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;35 – Pass functions, not strings, to setTimeout() and setInterval()&lt;/strong&gt;&lt;br&gt;
If you pass a string into &lt;strong&gt;setTimeout()&lt;/strong&gt; or &lt;strong&gt;setInterval()&lt;/strong&gt;, the string will be evaluated the same way as with eval, which is slow. Instead of using…&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;setInterval('doSomethingPeriodically()', 1000);  
setTimeout('doSomethingAfterFiveSeconds()', 5000);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;…use…&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;setInterval(doSomethingPeriodically, 1000);  
setTimeout(doSomethingAfterFiveSeconds, 5000);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>javascriptlibraries</category>
      <category>coding</category>
    </item>
  </channel>
</rss>
