Introduction
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.
Description
Quick best practice tips:
- Use === while comparing two variables instead of ==
- Remember undefined is not null
- Remember javascript falsy value: 0, '', NaN, null, undefined
- Remember javascript truthy value: '0', 'any string', [] (Empty array), {} (Empty object), 0 (any non-zero number)
- Always declare all variables at the beginning of every scope
- "use strict"; In javascript to avoid unwanted bug due to a variable.
- Avoid global variable declaration
- Reduce globals e.g var name='abc', isValid=false; Should be writen as var common={name:'abc', isValid:false};
- Always declare local variables
- Never declare Number, String or Boolean Objects ( e.g. Never use: new Number(1), new String("abc"), new Boolean() )
- Use {} instead of new Object()
- Use "" instead of new String()
- Use 0 instead of new Number()
- Use false instead of new Boolean()
- Use [] instead of new Array()
- Use /()/ instead of new RegExp()
- Use function (){} instead of new Function()
- Avoid Using eval()
- Don't use short hand e.g Always use curly bracket with conditional operation
- Place scripts at the Bottom of page
- Declare Variables Outside of the from loops and conditionals (such as if, for, while, switch and try)
- Properly comment your code
- Never pass a string to SetInterval and SetTimeOut. Instead, pass a function name.
- Always, Always Use Semicolons
Conclusion
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
- https://autotelicum.github.io/Smooth-CoffeeScript/literate/js-intro.html
- https://www.w3schools.com/js/js_hoisting.asp
- https://www.w3schools.com/js/js_best_practices.asp
- https://www.w3schools.com/js/js_mistakes.asp
- https://code.tutsplus.com/tutorials/24-javascript-best-practices-for-beginners--net-5399\

Sagar Pandurang KapPosted Mar 13, 2018, 12:03 AM
Very helpful tricks.....