July 27, 2017

JavaScript Best Practice Tips

In this article, I'll share important factors to be taken into considerations while working with JavaScript. This information will help you to prevent most common mistakes in web development.

Quick Tips
  1. Use === while comparing two variable instead of ==
  2. Remember undefined is not null
  3. Remember JavaScript falsy value:   0, '', NaN, null, undefined
  4. Remember JavaScript truthy value:   '0', 'any string', [] (Empty array), {} (Empty object), 0 (any non-zero number)
  5.  Always declare all variables at the beginning of every scope
  6.  "use strict"; In JavaScript to avoid unwanted bug due to a variable.
  7. Avoid global variable declaration
  8. Reduce global variables e.g var name='abc', isValid=false; Should be written as var common={name:'abc', isValid:false};
  9. Always declare local variables
  10. Never declare Number, String or Boolean Objects ( e.g. Never use: new Number(1), new String("abc"), new Boolean() )
  11. Use {} instead of new Object()
  12. Use "" instead of new String()
  13. Use 0 instead of new Number()
  14. Use false instead of new Boolean()
  15. Use [] instead of new Array()
  16. Use /()/ instead of new RegExp()
  17. Use function (){} instead of new Function()
  18. Avoid Using eval()
  19. Don't use short hand (e.g Always use curly bracket with conditional operation)
  20. Place scripts at the Bottom of the page
  21. Declare variables outside of the loops and conditionals (such as if, for, while, switch and try)
  22. Properly comment your code
  23. Never pass a string to SetInterval and SetTimeOut. Instead, pass a function name
  24. Always, Always Use Semicolons
Reference Links
If you have any questions you can reach out our SharePoint Consulting team here.

No comments:

Post a Comment