JavaScript Best Practices

Getting things done is good but getting things done in better way is what matters most. Everyone wants to make their website/application to be faster, lighter. No one likes slow website/application. In order to make website/application we have to set some best practices standards and every teammate should follow that.

I have written down few of the best practices which I recommend to be followed in any JavaScript development.

  1. Use === Instead of ==
  2. Eval = Evil
  3. Don't use short-hand
                         Bad
    Recommended
    if(condition)
         x = false;
     if(condition){
    
         x = false;
    
     }




  4. Place scripts at the bottom of your page.
  5. The fastest way to build a HTML list.
    var arr = ['item 1', 'item 2', 'item 3', ...];
    var list = '<ul><li>'+arr.join('</li><li>')+'</li></ul>';
  6. Reduce global variables and methods.
  7. Add comments on method level and conditions ( if complex condion statements are there ).
  8. Use {} Instead of New Object().
  9. Use [] Instead of New Array().
  10. Whenever we have long list of variables, omit the "var" keyword and use commas instead
  11. Always use semicolons to end the statement
  12. Vanila JS is always quicker than using a library. Use Vanila JS as possible if you want highest performance.
  13. Use single quotes instead of double quotes for strings.

Comments

Popular posts from this blog

Cheat Sheet - Git

Stack implementation using VanillaJS