Skip to content
XhmikosR edited this page Mar 4, 2014 · 4 revisions

Put all requires on top


In case of blocks with 1 statement you can easily get rid of braces, e.g.

if (true)
  doSometing();

When iterating an array cache its length in a local variable, e.g.

for (var i = 0, l = array.length; i < l; i++) {
  ...
}

Always put else or else if in the same line as a closing brace, e.g.

if (idx > 1) {
  doSomething();
} else if (idx === 1) {
  doSomethingElse();
} else {
  justDo();
}

Use guard pattern to reduce indentation, e.g.

function doSomething(x) {
  if (x < 1)
    return;

  ...
}

or

for (var property in object) {
  if (!object.hasOwnProperty(property))
    continue;
  
  ...
}

Clone this wiki locally