Use Spaces (Soft Tab), Not Tabs (Hard Tab)
// Good
var aVar = 123 ;
var otherVar = 'another' ;
// Bad
var aVar = 123 ; // Note hard tabs.
var otherVar = 'another' ;
// Good
var aVar = 123 ; // Note aligned at 16th column.
var otherVar = 'another' ;
// Bad
var aVar = 123 ; // Note aligned at 14th column.
var otherVar = 'another' ;
// Good
var foo = 123 ;
var bar = 'another' ;
// Bad
var foo = 123 ,
bar = 'another' ;
// Bad
var foo = 123
, bar = 'another'
;
Align at the Equal Sign Using Tab-Stops
// Good
var aVariable = 123 ;
var otherVariable = 'another' ;
// Bad
var aVariable = 123 ;
var otherVariable = 'another' ;
// Bad
var aVariable = 123 ;
var otherVariable = 'another' ;
// Bad
var aVariable = 123 ;
var otherVariable = 'another' ;
At Least One Space Either Side of Operator
// Good
foo = 'value' ;
// Bad
foo = 'value' ;
// Bad
foo = 'value' ;
// Bad
foo = 'value' ;
Always Wrap Body in Braces
// Good
if ( condition ) {
doSomething ( ) ;
}
// Bad
if ( condition )
doSomething ( ) ;
// Bad
if ( condition ) doSomething ( ) ;
// Good
while ( condition ) {
doSomething ( ) ;
}
// Bad
while ( condition ) { doSomething ( ) ; }
Open Brace On Keyword Line
// Good
for ( condition ) {
doSomething ( ) ;
}
// Bad
for ( condition )
{
doSomething ( ) ;
}
Closing Brace Gets Own Line
// Good
if ( condition ) {
doSomething ( ) ;
}
else {
doSomethingElse ( ) ;
}
// Bad
if ( condition ) {
doSomething ( ) ;
} else {
doSomethingElse ( ) ;
}
// Bad
if ( condition )
{
doSomething ( ) ;
} else
{
doSomethingElse ( ) ;
}
At Least One Space Inside
// Good
if ( someVar == true ) {
// Do something.
}
aFunc ( param ) ;
// Bad
if ( someVar == true ) {
// Do something.
}
aFunc ( param ) ;
No Space Outside Next To Keywords
Opening Brace On Line With Variable or Function
// Good
var foo = {
field1 : 123 ,
field2 : 'another'
} ;
someFunction ( {
field1 : 123 ,
field2 : 'another'
} ) ;
// Okay
var foo = { field1 : 123 } ; // Note only 1 field in object.
someFunction ( { field : 123 } ) ;
// Bad
var foo = { field1 : 123 , field2 : 'another' } ;
someFunction ( { field1 : 123 , field2 : 'another' } ) ;
// Bad
var foo =
{
field1 : 123 ,
field2 : 'another'
} ;
someFunction (
{
field1 : 123 ,
field2 : 'another'
}
) ;
Align on the Colon Using Tab-Stops
// Good
var obj = {
aField : 123 ,
anotherField : 'another'
} ;
// Bad
var obj = {
aField : 123 ,
anotherField : 'another'
} ;
// Bad
var obj = {
aField : 123 ,
anotherField : 'another'
} ;
// Bad
var obj = {
aField : 123 ,
anotherField : 'another'
} ;
// Good
var foo = require ( 'foo' ) ;
var other = require ( 'other' ) ;
var something = require ( 'something' ) ;
// Good
var foo = fooFunc ( 'foo' ) ;
var other = otherFunc ( 'other' ) ;
var something = someFunc ( 'something' ) ;
// Okay
var foo = require ( 'foo' ) ;
var other = require ( 'other' ) ;
var something = require ( 'something' ) ;
// Bad
var foo = fooFunc ( 'foo' ) ;
var other = otherFunc ( 'other' ) ;
var something = someFunc ( 'something' ) ;
Separate Definitions With A Horizontal Line
// Good
function foo ( ) {
doSomeStuff ( ) ;
}
// ---------------------------------------------------------------------------------------------- //
function bar ( ) {
doOtherStuff ( ) ;
}
// Good
var AClass = ( function ( ) {
function AClass ( ) {
}
// ------------------------------------------------------------------------------------------ //
AClass . prototype . aMethod = function ( ) {
doStuff ( ) ;
} ;
// ------------------------------------------------------------------------------------------ //
return AClass ;
} ) ( ) ;
// Bad
function foo ( ) {
doSomeStuff ( ) ;
}
function bar ( ) {
doOtherStuff ( ) ;
}
Documentation and Comments
Use JSDoc-Style Documentation
Use C++-Style Comments
Put a Comment Every 4-8 Lines
Always End Lines With Semicolon