JavaScript Tips And Tricks


In this blog, we will learn about important considerations while we are working with JavaScript.This information will help you to prevent most of the common mistakes during development.
Quick best practice tips:
  1. Use === while comparing two variables 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 globals e.g var name='abc', isValid=false; Should be writen 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 page
  21. Declare Variables Outside of the from 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


In this article, we got an idea of lots of JavaScript tips and tricks and best practices to improve our JavaScript programming skills.
Reference links