Always End a Statement with Semicolon
Single Quotation or Double Quotation for String Values
There is no difference either you use a single quotation or double quotations to represent a string. Even you can use both in a program, but it is recommended to use only one of them in a project. I personally recommend using a single quotation.
Explicit Variable Declaration
Use Strict Mode
Avoid Expensive Task in Main Thread
If you check any of the following in condition, they will return false: null, false, 0 undefined, NaN, and empty string. It is interesting that Infinity will return true.
- Grouping ()
- Arithmetic Operators
- Comparison Operators
- AND && Operator
- OR || Operators
Short Circuit Operators
Immediately Invoked Function Expressions (IIFE)
Immediately Invoked Function Expressions (IIFE) are executed right after loading automatically.
- (function sayHello()
Working with JSON
Constructor for Custom Objects
It is recommended to use Constructor to initialize and create objects instead of creating an object by directly assigning value to properties dynamically. It will make your code clean, easy to maintain, and faster. Changing the schema of an object is relatively slow.
Strict Equality Operator === is preferred over Equality Operator ==
- String.prototype.lengthSquare = function()
- return this.Length * this.length;
It is preferred to use === over == until unless we don’t have to compare data of different types. Furthermore, in general === is considered to have better performance.
- Use split instead of delete to remove an element from the array.
- Use push to add a new element at end of the array, by the way, myArray[myArray.length]=somevalue; will do the same.
- Use unshift to insert a new element at the start of the array.
- On arrays use a simple index-based loop instead of a loop iterator. A simple loop may give better performance.
Default Parameter Value
- function myFunction(parameter)
- parameter = parameter || 0;
Module Pattern is a very good option if you want to control access to functions and data elements. Furthermore, Modules are very helpful if you want to create some common custom repository or common library in your project.
- Cache references, e.g. save a reference to call in variable and then use this variable again and again instead of calling method to get reference again.
- When playing with control addition dynamically, it is better to initialize all required properties of control before adding to DOM.
- For existing objects, make display: none before changes, and then restore old value of display.
Minification and Bundling
Commenting and Beautification
- It is better to place script reference and or code at the bottom of HTML.
If someone is interested to master a tool then it is always required to explore a tool thoroughly. I may recommend reviewing the following topics again (every item is a complete topic):
- Error Handling
- Regular Expressions
- Object-Oriented Programming
- Module Pattern
- Browser API
- Local Storage
- Offline Browsing
- Form and Client-Side Validation
- Animation, Graphics, SVG, Canvas
- Web Workers
- It is always good to device some naming conventions. We will have a detailed manuscript on this topic in a future article.
- Proper code indention and region where possible.
- We may use a switch instead of if cascade if wherever possible because it is cleaner and faster.
- String concatenation is an expensive action.
- Be specific to use eval(). Use this feature as the last option as it is slow and has security issues.
- Avoid the statement, its usage is generally discouraged. It is forbidden in strict mode.
- Declare variables outside the loop.
- It is better to use Custom objects instead of defining too many global variables.
- If you are interested in code high-performance application then try to avoid dynamically typing. Simple, use the same type of variables in an expression.
- I may highly recommend, to get prepared for ECMAScript 6. If possible then try to code close to this standard, this habit may help you a lot in the future to migrate your existing code very easily.