- Objects
- Array
- Strings
- Variables
- Whitespace
- Commas
- Semicolons
- Block Statements
- Conditional Statements
- Properties
- Functions
- Arrow Functions
- Function Arguments
- Rest Parameters
- Destructuring
- Comments
- Use literal form for object creation.
var foo = {};
- Pad single-line objects with white-space.
var bar = { color: 'orange' };
- Use literal form for array creation (unless you know the exact length).
var foo = [];
- If you know the exact length and know that array is not going to grow, use
Array
.
var foo = new Array(16);
- Use
push
to add an item to an array.
var foo = [];
foo.push('bar');
- Use
'single quotes'
.
- Put all non-assigning declarations on one line.
var a, b;
- Use a single
var
declaration for each assignment.
var a = 1;
var b = 2;
- Declare variables at the top of their scope.
function foo() {
var bar;
console.log('foo bar!');
bar = getBar();
}
- Use soft tabs set to 2 spaces.
function() {
∙∙var name;
}
- Place 1 space before the leading brace.
obj.set('foo', {
foo: 'bar'
});
test('foo-bar', function() {
});
- No spaces before semicolons.
var foo = {};
- Keep parenthesis adjacent to the function name when declared or called.
function foo() {
}
foo();
- Spaces are required around binary operators.
// assignments
var foo = bar + 'a';
// conditionals
if (foo === 'bara') {
}
// parameters
function(test, foo) {
}
- Skip trailing commas.
var foo = [1, 2, 3];
var bar = { a: 'a' };
- Skip leading commas.
var foo = [
1,
2,
3
];
- Use semicolons.
- Use spaces.
// conditional
if (notFound) {
return 0;
} else {
return 1;
}
switch (condition) {
case 'yes':
// code
break;
}
// loops
for (var key in keys) {
// code
}
for (var i = 0, l = keys.length; i < l; i++) {
// code
}
while (true) {
// code
}
try {
// code that throws an error
} catch(e) {
// code that handles an error
}
- Opening curly brace should be on the same line as the beginning of a statement or declaration.
function foo() {
var obj = {
val: 'test'
};
return {
data: obj
};
}
if (foo === 1) {
foo();
}
for (var key in keys) {
bar(e);
}
while (true) {
foo();
}
- Keep
else
and its accompanying braces on the same line.
if (foo === 1) {
bar = 2;
} else {
bar = '2';
}
if (foo === 1) {
bar = 2;
} else if (foo === 2) {
bar = 1;
} else {
bar = 3;
}
- Use
===
and!==
. - Use curly braces.
if (notFound) {
return;
}
- Use explicit conditions.
if (arr.length > 0) {
// code
}
if (foo !== '') {
// code
}
- Use dot-notation when accessing properties.
var foo = {
bar: 'bar'
};
foo.bar;
- Use
[]
when accessing properties with a variable.
var propertyName = 'bar';
var foo = {
bar: 'bar'
};
foo[propertyName];
- Make sure to name functions when you define them.
function fooBar() {
}
- Make sure arrow functions are done on multiple lines.
var foo = [1,2,3,4].map((item) => {
return item * 2;
});
arguments
object must not be passed or leaked anywhere.
See the reference.
- Use a
for
loop witharguments
(instead ofslice
).
function fooBar() {
var args = new Array(arguments.length);
for (var i = 0; i < args.length; ++i) {
args[i] = arguments[i];
}
return args;
}
- Don't re-assign the arguments.
function fooBar() {
arguments = 3;
}
function fooBar(opt) {
opt = 3;
}
- Use a new variable if you need to re-assign an argument.
function fooBar(opt) {
var options = opt;
options = 3;
}
Since Babel implements Rest parameters in a non-leaking matter you should use them whenever applicable.
function foo(...args) {
args.forEach((item) => {
console.log(item);
});
}
When decomposing simple arrays or objects, prefer destructuring.
// array destructuring
var fullName = 'component:foo-bar';
var [
first,
last
] = fullName.split(':');
// object destructuring
var person = {
firstName: 'Stefan',
lastName: 'Penner'
};
var {
firstName,
lastName
} = person;
- Use YUIDoc comments for documenting functions.
- Use
//
for single line comments.
function foo() {
var bar = 5;
// multiplies `bar` by 2.
fooBar(bar);
console.log(bar);
}