-
Notifications
You must be signed in to change notification settings - Fork 2
FreeCodeCamp Style Guide
or How Cool People Write JavaScript.
Always Use Two Spaces
No hard tabs, ever. No really, just don't do it.
Always use curly braces when using the keywords if/else/else if
. This prevents a lot of ambiguity and will prevent syntax errors in some edge cases.
Bad:
if (foo) bar();
Good:
if (foo) { bar(); }
Space After function
Keyword, Except in Anonymous Functions
Good:
var foo = function() {
// ...
};
function foo() {
}
Bad:
var foo = function () {
// ...
};
function foo ()
{
// ...
}
- no inline comments
- single space after
//
- Do not use multiline comment
/* */
, we are reserving these for use with jsDocs.
- space immediately after if, else, while, etc
- opening curly brace should always be on the same line.
Good:
if (true) {
// do the thing
}
Bad:
if(true)
{
// do the thing
}
Avoid else and "end early". In JavaScript there is often a lot of indenting (usually when dealing with async code and named "callback hell"). Anything you can do reduce the number of indents should be done. One thing is to avoid the else keyword.
This also has the side effect of making code cleaner and easier to read.
Bad:
someAsynFunc(function(err, data) {
if (err) {
callback(err);
} else {
// do stuff with data
}
});
Good:
someAsynFunc(function(err, data) {
if (err) {
return callback(err);
}
// do stuff with data
// saves one indent
});
Long multiline strings should be in one of two forms:
var longString =
‘long strings should ‘ +
‘be in this form, with the ‘ +
‘operator ending the line’;
var foo = 'bar';
var longString = [
'long strings with variables such as ',
foo,
'should ',
'be in this form, an array of strings ',
'that are joined with the join array instance method',
].join('');
...more to come
Learn to code and help nonprofits. Join our open source community in 15 seconds at http://freecodecamp.com
Follow our Medium blog
Follow Quincy on Quora
Follow us on Twitter
Like us on Facebook
And be sure to click the "Star" button in the upper right of this page.
New to Free Code Camp?
JS Concepts
JS Language Reference
- arguments
- Array.prototype.filter
- Array.prototype.indexOf
- Array.prototype.map
- Array.prototype.pop
- Array.prototype.push
- Array.prototype.shift
- Array.prototype.slice
- Array.prototype.some
- Array.prototype.toString
- Boolean
- for loop
- for..in loop
- for..of loop
- String.prototype.split
- String.prototype.toLowerCase
- String.prototype.toUpperCase
- undefined
Other Links