You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Don't leave a space between object properties (if they are not functions)
Don't insert meaningless line breaks
// Badfunction(car){varcolor=car.color;if(color==='red'){return'Car is red';}return'Car is not red';}// Badfunction(car){varcolor=car.color;if(color==='red'){return'Car is red';}return'Car is not red';}// Badfunction(car){varcolor=car.color;if(color==='red'){return'Car is red';}return'Car is not red';}// Goodfunction(car){varcolor=car.color;if(color==='red'){return'Car is red';}return'Car is not red';}// Badvarobject={foo: function(){// ...},bar: function(){// ...}};returnobject;// Goodvarobject={foo: function(){// ...},bar: function(){// ...}};returnobject;// Badvarobject={foo: 'some property',bar: 'other property',other: 'yes another property'}// Goodvarobject={foo: 'some property',bar: 'other property',other: 'yes another property'}
Don't leave white spaces at the end of lines. (~ for whitespace)
Empty lines should not have white spaces.
// Empty spaces are represented by ~// Badfunction(car){varcolor=car.color;~~if(color==='red'){return'Car is red';}~~return'Car is not red';}// Badfunction(){~varcolor=car.color;~~if(color==='red'){return'Car is red';~}return'Car is not red';}// Goodfunction(){varcolor=car.color;if(color==='red'){return'Car is red';}return'Car is not red';}
// Badif(car.color==='red')return'Car is red';// Badif(car.color==='red'){return'Car is red';}// Badif(car.color==='red')return'Car is red';// Goodif(car.color==='red'){return'Car is red';}
Put else statement on the same line as the closing bracket of the if statement.
The code should be clear enough not to need comments.
Put an empty line before comments.
// Badvarcar=newCar();// Do some complex operation with carcar.someComplexOperation(...);// Goodvarcar=newCar();// Do some complicated operation with carcar.someComplexOperation(...);
Use // FIXME: to annotate problems
functionCalculator(){// FIXME: shouldn't use a global heretotal=0;returnthis;}
Use // TODO: to annotate solutions to problems
functionCalculator(){// TODO: total should be configurable by an options paramthis.total=0;returnthis;}
Strings
Use single quotes for strings
// Badvarcolor="red";// Goodvarcolor='red';
Use concatenation when the line is more than 80 characters
If the string length is too long, use Array#join instead of concatenation.
// Badvarmessage='This is a very long message which should not be written in a single line';// Badvarmessage='This is a very long message which should not be \written in a single line';// Goodvarmessage='This is a very long message which should not be '+'written in a single line';
Operators
Use identity instead of equality. If needed, use explicit type conversion.
// Badfunction(color){if(color==='red'){varmessage='Car is red';}// ...}// Goodfunction(color){varmessage;if(color==='red'){message='Car is red';}// ...}
Type conversion
Use explicit type conversion when converting to integer.
Always provide the second argument when using parseInt.
Instantiate new arrays using the literal notation. The constructor's signature is ambiguous as new Array(2) will make an array of size 2, and new Array('2') will make an array with the string '2' as value.
JavaScript arrays are objects. Their lenght is always dynamic, thus size doesn't need to be specified.
When a function can only execute with certain arguments, use a conditional at the top to immediately return.
// BadfunctionregisterCar(car){if(car){// code to register the car ...}else{return;}}// GoodfunctionregisterCar(car){if(!car){return;}// code to register the car ...}
Properties
Use dot notation for accessing properties
// Badcar['color']='red';// Goodcar.color='red';
Use brackets when accessing properties via a variable