-
Variable declarations should always be made using var to not declare them as global variables.
function sum(x, y) { result = x + y; return result; }
function sum(x, y) { var result = x + y; return result; }
-
use camelCase for variables and functions.
-
Constants (like PI) are written in UPPERCASE.
-
JavaScript files should have a .js extension.
-
Constructors intended for use UpperCamelCase.
-
Always end a simple statement with a semicolon.
-
In case of object use colon plus one space between each property and its value.
-
In case of keys in object use quotes around string values, not around numeric values.
-
Do not add a comma after the last property-value pair of an object.
-
For understandability try to declare all variables at the top.
-
To check both data type and values use '==='.
var a = "5"; var b = 5;
if (a==b && typeof(a) == typeof(b)) { // do something }
var a = "5"; var b = 5;
if (a===b) { // do something
}
-
Use 2 space indentation.
-
Always put spaces around operators ( = + - * / ), and after commas.
-
Always put the opening brace on the same line as the previous statement.
function func() { return { "name": "Xyz" }; }
function func () { return { "name": "Xyz" }; }
-
whatever pattern you follow to place the braces, same pattern should be followed in all the places through-out the project
-
The closing brace should be on the same indent as the original function call.
function func() { return { "name": "Xyz" }; }
function func() { return { "name": "Xyz" }; }
-
better to use
if/else
conditions over other ternary conditions though it is small. -
Don't use an empty line at the beginning or end of methods, blocks or conditionals. Use an empty line between methods, blocks and conditionals.
if some_condition {
// do something }
if some_condition { // do something }
[2, 3, 4].map( function check(x) { return x*2; }) if some_condition { // do some thing }
[2, 3, 4].map( function check(x) { return x*2; })
if some_condition { // do some thing }
-
When a conditional is too long to fit on one line, successive lines must be indented one extra level to distinguish them from the body.
if ( firstCondition() && secondCondition() && thirdCondition() ) { doStuff(); }
if ( firstCondition() && secondCondition() && thirdCondition() ) { doStuff(); }
-
Switch cases are allowed to share the same code block. Example: switch (some_condition) { case 4: case 5: // do something break; }
-
Javascript array can have data of different data types. Example: var a = [1,"a",2];