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.
I have written down few of the best practices which I recommend to be followed in any JavaScript development.
- Use === Instead of ==
- Eval = Evil
- Don't use short-hand
BadRecommendedif(condition) x = false;if(condition){ x = false; }
- Place scripts at the bottom of your page.
- 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>'; - Reduce global variables and methods.
- Add comments on method level and conditions ( if complex condion statements are there ).
- Use {} Instead of New Object().
- Use [] Instead of New Array().
- Whenever we have long list of variables, omit the "var" keyword and use commas instead
- Always use semicolons to end the statement
- Vanila JS is always quicker than using a library. Use Vanila JS as possible if you want highest performance.
- Use single quotes instead of double quotes for strings.
Comments
Post a Comment