Introduction
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
- Use === while comparing two variable 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 global variables e.g var name='abc', isValid=false; Should be written 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 the page
- Declare variables outside of the 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
Reference Links
No comments:
Post a Comment