Thursday, July 19, 2018

These 10 Useful JavaScript Tips & Tricks That Every JavaScript Developer Should Know


   Here we talk about 10 JavaScript tips & tricks that every programmer should know, These tips & tricks are very useful to every JavaScript developer, these tips & tricks will help you to brush up your programming skills.

1 - Use Semicolons at the end of the line for termination :
As every programmer know about the value of Semicolon in programming world, Semicolon is used at the end of the line for line termination, But if we did not use Semicolon in JavaScript its ok with it JavaScript will not going to  give you an error if you forget to use Semicolon, But its a good practice to use Semicolon at the end of the line for line termination.

2 - Use = = = instead of = = :
Here = = = and = = are two different thing in JavaScript, You should aware about this difference in both operators, The = = operator performs an automatic type conversion if needed.

3 - Don't forgot to use var keyword to assigning a variable's value first time :
var keyword is use for creating a variable in JavaScript, you can also assign your variable at the creating time, you should avoid global variable in JavaScript.
                     
                                   var name = " Syntaxios " ;                                                           
                                   document.write(name);                                                                                                       

4 - Avoid eval() :
eval() also called function constructor, eval() always called script engine each time to convert a source code into a executable code, That's by you always try to avoid this function in JavaScript.

                               var function1 = new Function (functionCode);                                         
                               var function2 = eval(functionCode);                                                                          
 
5 - Avoid using with() :
The with() is used for insert a variable at the global scope, Thus, If you have another variable with same name the this could cause confusion and may be overwrite the value of your another variable.

6 - Create an object constructor :
                         function Person(firstName, lastName){                                       
                                     this.firstName = firstName;                          
                                    this.lastName = lastName;                     
                             }                                                                        
                    var Aman = new Person("Aman","Singh");                     
                   //Here we create an object Aman                                                                                        

7 - Create a Self-Calling Function :
Self-Calling function also called Self-invoke anonymous function or immediately invoked function. The Self-Calling function always execute automatically when you create it.

                          (function(){                                                                                       
    //Put your code here that you want to execute or call automatically  
                           })();                                                                    
                        (function(a,b){                                               
                                     var answer = a+b;                                     
                                     return answer;                                            
                          })( 7, 5 )                                                                                                                           

8 - Get a Random Number between specific range :
To generate a random number between the specified range you have to import the Math library, Math library have random function that is used to get a random number in a specified range, This code is useful when you want to generate some fake data for testing like age between min age than max age.

                              var age = Math.floor(Math.random()*(max - min + 1))+min;                              

9 - Get random item from an array :
This code will help you to get random number or item from an array.

                              var items = [1, 440, 'aman', 2, 530, 'singh', 3421 119, 'rajawat', 5, 765];  
                              var random_items = items[Math.floor(Math.random()* items.length)]; 

10 - Generate an array of numbers zero to max :
This code will generate the array of numbers with numbers from zero to max,<
               var number_array = [];                                                                      
           var max = 100; // maximum number upto 100, max = 100;    
                        for ( var i=1; number_array.push(i++)< max);          
                    //Numbers = [ 1, 2, 3, 4, 5, ......., 100 ];                                                                  
, , , , ,

3 comments: