Based off of AirBnB's style guide.
Keeping our JavaScript more consistent
- Strings
- Variables
- Semicolons
- Commas
- Declaring Strict Mode
- Naming Conventions
- Conditional Expressions
- Blocks
- Tab Spacing
- Types
- Objects
- Arrays
- Properties
- Accessors
- Tools
- License
Use single quotes ''
for strings instead of double quotes ""
// bad
var name = "Eva Note";
var fullName = "Eva " + this.lastName;
// good
var name = 'Eva Note';
var fullName = 'Eva ' + this.lastName;
Declarations for var
as follows:
// bad
var a = 1;
var b = 2;
var c = 'string';
// good
var a = 1,
b = 2,
c = 'string';
Use semicolons ;
// bad
function() {
return 'Evernote'
}
// good
function() {
return 'Evernote';
}
Don't use leading commas.
// bad
var evernote
, premium
, business;
// good
var evernote,
premium,
business;
// bad
var product = {
name: 'Evernote'
, type: 'App'
, category: 'Productivity'
};
// good
var product = {
name: 'Evernote',
type: 'App',
category: 'Productivity'
};
// good
function() {
return 'Evernote';
}
- Use descriptive naming. Avoid single letter names.
// bad
function t() {
// do something...
}
// good
function test() {
// do something...
}
- Use camelCase when naming objects, functions, and instances.
// bad
var OBject = {};
var heres_an_object = {};
function c() {}
var u = new user({
name: 'Evernote';
});
// good
var thisIsMyObject = {};
function thisIsMyFunction() {}
var user = new User({
name: 'Evernote';
});
Declare 'use strict'
in all your JavaScript files.
##Conditional Expressions
Use ===
and !==
// bad
var name = 'Eva Note';
if(name == 'Eva Note'){}
if(name != 'Eva Note'){}
// good
var name = 'Eva Note';
if(name === 'Eva Note'){}
if(name !== 'Eva Note'){}
##Blocks
Use braces when there are multi-line blocks.
// bad
if (name)
return false;
// good
if (name) return false;
// bad
if (name){return false};
// good
if (name){
return false;
}
##Tab Spacing
Use two spaces for tabs
// bad
function() {
∙∙∙∙return true;
}
// good
function() {
∙∙return true;
}
-
Primitives: When you access a primitive type you work directly on its value
string
number
boolean
null
undefined
var foo = 1, bar = foo; bar = 9; console.log(foo, bar); // => 1, 9
-
Complex: When you access a complex type you work on a reference to its value
object
array
function
var foo = [1, 2], bar = foo; bar[0] = 9; console.log(foo[0], bar[0]); // => 9, 9
-
Use the literal syntax for object creation.
// bad var item = new Object(); // good var item = {};
-
Don't use reserved words as keys. It won't work in IE8.
// bad var superman = { default: { clark: 'kent' }, private: true }; // good var superman = { defaults: { clark: 'kent' }, hidden: true };
-
Use readable synonyms in place of reserved words.
// bad var superman = { class: 'alien' }; // bad var superman = { klass: 'alien' }; // good var superman = { type: 'alien' };
-
Use the literal syntax for array creation
// bad var items = new Array(); // good var items = [];
-
If you don't know array length use Array#push.
var someStack = []; // bad someStack[someStack.length] = 'abracadabra'; // good someStack.push('abracadabra');
-
When you need to copy an array use Array#slice. jsPerf
var len = items.length, itemsCopy = [], i; // bad for (i = 0; i < len; i++) { itemsCopy[i] = items[i]; } // good itemsCopy = items.slice();
-
To convert an array-like object to an array, use Array#slice.
function trigger() { var args = Array.prototype.slice.call(arguments); ... }
-
Use dot notation when accessing properties.
var evernote = { elephant: true, remember: true }; // bad var isElephant = evernote['elephant']; // good var isElephant = evernote.elephant;
-
Use subscript notation
[]
when accessing properties with a variable.var evernote = { elephant: true, remember: true }; function getProp(prop) { return evernote[prop]; } var isElephant = getProp('elephant');
-
Accessor functions for properties are not required
-
If you do make accessor functions use getVal() and setVal('hello')
// bad elephant.age(); // good elephant.getAge(); // bad elephant.age(25); // good elephant.setAge(25);
-
If the property is a boolean, use isVal() or hasVal()
// bad if (!elephant.age()) { return false; } // good if (!elephant.hasAge()) { return false; }
(The MIT License)
Copyright (c) 2014 Airbnb
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.