- Tab indentation
- Single-quotes
- Semicolon
- camelCase
- Strict mode
- No trailing whitespace
- Variables where needed
- Multiple variable statements
- Space after keywords and between arguments and operators
- Use === and !== over == and !=.
- Return early
- Limit line lengths to 120 chars
- Prefer readability over religion
Example:
'use strict';
function foo(bar, fum) {
if (!bar) return;
var hello = 'Hello';
var ret = 0;
for (var barIdx = 0; barIdx < bar.length; barIdx++) {
if (bar[barIdx] === hello) {
ret += fum(bar[barIdx]);
}
}
return ret;
}
Read idiomatic.js for general JavaScript code style best practices.