Home

Awesome

Airbnb JavaScript Style Guide() {

A mostly reasonable approach to JavaScript

Downloads Downloads ![Gitter](https://badges.gitter.im/Join Chat.svg)

Other Style Guides

Table of Contents

  1. Types
  2. References
  3. Objects
  4. Arrays
  5. Destructuring
  6. Strings
  7. Functions
  8. Arrow Functions
  9. Classes & Constructors
  10. Modules
  11. Iterators and Generators
  12. Properties
  13. Variables
  14. Hoisting
  15. Comparison Operators & Equality
  16. Blocks
  17. Comments
  18. Whitespace
  19. Commas
  20. Semicolons
  21. Type Casting & Coercion
  22. Naming Conventions
  23. Accessors
  24. Events
  25. jQuery
  26. ECMAScript 5 Compatibility
  27. ECMAScript 6 Styles
  28. Testing
  29. Performance
  30. Resources
  31. In the Wild
  32. Translation
  33. The JavaScript Style Guide Guide
  34. Chat With Us About JavaScript
  35. Contributors
  36. License

Types

<a name="types--primitives"></a><a name="1.1"></a>

<a name="types--complex"></a><a name="1.2"></a>

⬆ back to top

References

<a name="references--prefer-const"></a><a name="2.1"></a>

<a name="references--disallow-var"></a><a name="2.2"></a>

<a name="references--block-scope"></a><a name="2.3"></a>

⬆ back to top

Objects

<a name="objects--no-new"></a><a name="3.1"></a>

<a name="es6-computed-properties"></a><a name="3.4"></a>

<a name="es6-object-shorthand"></a><a name="3.5"></a>

<a name="es6-object-concise"></a><a name="3.6"></a>

<a name="objects--grouped-shorthand"></a><a name="3.7"></a>

<a name="objects--quoted-props"></a><a name="3.8"></a>

Why? In general we consider it subjectively easier to read. It improves syntax highlighting, and is also more easily optimized by many JS engines.

// bad
const bad = {
  'foo': 3,
  'bar': 4,
  'data-blah': 5,
};

// good
const good = {
  foo: 3,
  bar: 4,
  'data-blah': 5,
};

<a name="objects--prototype-builtins"></a>

Why? These methods may be shadowed by properties on the object in question - consider { hasOwnProperty: false } - or, the object may be a null object (Object.create(null)).

// bad
console.log(object.hasOwnProperty(key));

// good
console.log(Object.prototype.hasOwnProperty.call(object, key));

// best
const has = Object.prototype.hasOwnProperty; // cache the lookup once, in module scope.
/* or */
const has = require('has');
…
console.log(has.call(object, key));

⬆ back to top

Arrays

<a name="arrays--literals"></a><a name="4.1"></a>

<a name="arrays--push"></a><a name="4.2"></a>

<a name="es6-array-spreads"></a><a name="4.3"></a>

<a name="arrays--from"></a><a name="4.4"></a>

<a name="arrays--callback-return"></a><a name="4.5"></a>

⬆ back to top

Destructuring

<a name="destructuring--object"></a><a name="5.1"></a>

<a name="destructuring--array"></a><a name="5.2"></a>

<a name="destructuring--object-over-array"></a><a name="5.3"></a>

⬆ back to top

Strings

<a name="strings--quotes"></a><a name="6.1"></a>

<a name="strings--line-length"></a><a name="6.2"></a>

<a name="es6-template-literals"></a><a name="6.4"></a>

<a name="strings--eval"></a><a name="6.5"></a>

<a name="strings--escaping"></a>

⬆ back to top

Functions

<a name="functions--declarations"></a><a name="7.1"></a>

<a name="functions--iife"></a><a name="7.2"></a>

<a name="functions--in-blocks"></a><a name="7.3"></a>

<a name="functions--note-on-blocks"></a><a name="7.4"></a>

<a name="functions--arguments-shadow"></a><a name="7.5"></a>

<a name="es6-rest"></a><a name="7.6"></a>

<a name="es6-default-parameters"></a><a name="7.7"></a>

<a name="functions--default-side-effects"></a><a name="7.8"></a>

<a name="functions--defaults-last"></a><a name="7.9"></a>

<a name="functions--constructor"></a><a name="7.10"></a>

<a name="functions--signature-spacing"></a><a name="7.11"></a>

<a name="functions--mutate-params"></a><a name="7.12"></a>

<a name="functions--reassign-params"></a><a name="7.13"></a>

<a name="functions--spread-vs-apply"></a><a name="7.14"></a>

⬆ back to top

Arrow Functions

<a name="arrows--use-them"></a><a name="8.1"></a>

<a name="arrows--implicit-return"></a><a name="8.2"></a>

<a name="arrows--paren-wrap"></a><a name="8.3"></a>

<a name="arrows--one-arg-parens"></a><a name="8.4"></a>

<a name="arrows--confusing"></a><a name="8.5"></a>

⬆ back to top

Classes & Constructors

<a name="constructors--use-class"></a><a name="9.1"></a>

<a name="constructors--extends"></a><a name="9.2"></a>

<a name="constructors--chaining"></a><a name="9.3"></a>

<a name="constructors--tostring"></a><a name="9.4"></a>

<a name="constructors--no-useless"></a><a name="9.5"></a>

<a name="classes--no-duplicate-members"></a>

⬆ back to top

Modules

<a name="modules--use-them"></a><a name="10.1"></a>

<a name="modules--no-wildcard"></a><a name="10.2"></a>

<a name="modules--no-export-from-import"></a><a name="10.3"></a>

<a name="modules--no-duplicate-imports"></a>

<a name="modules--no-mutable-exports"></a>

<a name="modules--prefer-default-export"></a>

<a name="modules--imports-first"></a>

⬆ back to top

Iterators and Generators

<a name="iterators--nope"></a><a name="11.1"></a>

<a name="generators--nope"></a><a name="11.2"></a>

<a name="generators--spacing"></a>

⬆ back to top

Properties

<a name="properties--dot"></a><a name="12.1"></a>

<a name="properties--bracket"></a><a name="12.2"></a>

⬆ back to top

Variables

<a name="variables--const"></a><a name="13.1"></a>

<a name="variables--one-const"></a><a name="13.2"></a>

<a name="variables--const-let-group"></a><a name="13.3"></a>

<a name="variables--define-where-used"></a><a name="13.4"></a>

⬆ back to top

Hoisting

<a name="hoisting--about"></a><a name="14.1"></a>

<a name="hoisting--anon-expressions"></a><a name="14.2"></a>

<a name="hoisting--named-expresions"></a><a name="14.3"></a>

<a name="hoisting--declarations"></a><a name="14.4"></a>

⬆ back to top

Comparison Operators & Equality

<a name="comparison--eqeqeq"></a><a name="15.1"></a>

<a name="comparison--if"></a><a name="15.2"></a>

<a name="comparison--shortcuts"></a><a name="15.3"></a>

<a name="comparison--moreinfo"></a><a name="15.4"></a>

<a name="comparison--switch-blocks"></a><a name="15.5"></a>

Why? Lexical declarations are visible in the entire switch block but only get initialized when assigned, which only happens when its case is reached. This causes problems when multiple case clauses attempt to define the same thing.

eslint rules: no-case-declarations.

```javascript
// bad
switch (foo) {
  case 1:
    let x = 1;
    break;
  case 2:
    const y = 2;
    break;
  case 3:
    function f() {}
    break;
  default:
    class C {}
}

// good
switch (foo) {
  case 1: {
    let x = 1;
    break;
  }
  case 2: {
    const y = 2;
    break;
  }
  case 3: {
    function f() {}
    break;
  }
  case 4:
    bar();
    break;
  default: {
    class C {}
  }
}
```

<a name="comparison--nested-ternaries"></a><a name="15.6"></a>

<a name="comparison--unneeded-ternary"></a><a name="15.7"></a>

⬆ back to top

Blocks

<a name="blocks--braces"></a><a name="16.1"></a>

<a name="blocks--cuddled-elses"></a><a name="16.2"></a>

⬆ back to top

Comments

<a name="comments--multiline"></a><a name="17.1"></a>

<a name="comments--singleline"></a><a name="17.2"></a>

<a name="comments--actionitems"></a><a name="17.3"></a>

<a name="comments--fixme"></a><a name="17.4"></a>

<a name="comments--todo"></a><a name="17.5"></a>

⬆ back to top

Whitespace

<a name="whitespace--spaces"></a><a name="18.1"></a>

<a name="whitespace--before-blocks"></a><a name="18.2"></a>

<a name="whitespace--around-keywords"></a><a name="18.3"></a>

<a name="whitespace--infix-ops"></a><a name="18.4"></a>

<a name="whitespace--newline-at-end"></a><a name="18.5"></a>

<a name="whitespace--chains"></a><a name="18.6"></a>

<a name="whitespace--after-blocks"></a><a name="18.7"></a>

<a name="whitespace--padded-blocks"></a><a name="18.8"></a>

<a name="whitespace--in-parens"></a><a name="18.9"></a>

<a name="whitespace--in-brackets"></a><a name="18.10"></a>

<a name="whitespace--in-braces"></a><a name="18.11"></a>

<a name="whitespace--max-len"></a><a name="18.12"></a>

⬆ back to top

Commas

<a name="commas--leading-trailing"></a><a name="19.1"></a>

<a name="commas--dangling"></a><a name="19.2"></a>

⬆ back to top

Semicolons

<a name="semicolons--required"></a><a name="20.1"></a>

⬆ back to top

Type Casting & Coercion

<a name="coercion--explicit"></a><a name="21.1"></a>

<a name="coercion--strings"></a><a name="21.2"></a>

<a name="coercion--numbers"></a><a name="21.3"></a>

<a name="coercion--comment-deviations"></a><a name="21.4"></a>

<a name="coercion--bitwise"></a><a name="21.5"></a>

<a name="coercion--booleans"></a><a name="21.6"></a>

⬆ back to top

Naming Conventions

<a name="naming--descriptive"></a><a name="22.1"></a>

<a name="naming--camelCase"></a><a name="22.2"></a>

<a name="naming--PascalCase"></a><a name="22.3"></a>

<a name="naming--leading-underscore"></a><a name="22.4"></a>

<a name="naming--self-this"></a><a name="22.5"></a>

<a name="naming--filename-matches-export"></a><a name="22.6"></a>

<a name="naming--camelCase-default-export"></a><a name="22.7"></a>

<a name="naming--PascalCase-singleton"></a><a name="22.8"></a>

⬆ back to top

Accessors

<a name="accessors--not-required"></a><a name="23.1"></a>

<a name="accessors--no-getters-setters"></a><a name="23.2"></a>

<a name="accessors--boolean-prefix"></a><a name="23.3"></a>

<a name="accessors--consistent"></a><a name="23.4"></a>

⬆ back to top

Events

<a name="events--hash"></a><a name="24.1"></a>

⬆ back to top

jQuery

<a name="jquery--dollar-prefix"></a><a name="25.1"></a>

<a name="jquery--cache"></a><a name="25.2"></a>

<a name="jquery--queries"></a><a name="25.3"></a>

<a name="jquery--find"></a><a name="25.4"></a>

⬆ back to top

ECMAScript 5 Compatibility

<a name="es5-compat--kangax"></a><a name="26.1"></a>

⬆ back to top

ECMAScript 6 Styles

<a name="es6-styles"></a><a name="27.1"></a>

  1. Arrow Functions
  2. Classes
  3. Object Shorthand
  4. Object Concise
  5. Object Computed Properties
  6. Template Strings
  7. Destructuring
  8. Default Parameters
  9. Rest
  10. Array Spreads
  11. Let and Const
  12. Iterators and Generators
  13. Modules

⬆ back to top

Testing

<a name="testing--yup"></a><a name="28.1"></a>

<a name="testing--for-real"></a><a name="28.2"></a>

⬆ back to top

Performance

⬆ back to top

Resources

Learning ES6

Read This

Tools

Other Style Guides

Other Styles

Further Reading

Books

Blogs

Podcasts

⬆ back to top

In the Wild

This is a list of organizations that are using this style guide. Send us a pull request and we'll add you to the list.

⬆ back to top

Translation

This style guide is also available in other languages:

The JavaScript Style Guide Guide

Chat With Us About JavaScript

Contributors

License

(The MIT License)

Copyright (c) 2014-2016 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.

⬆ back to top

Amendments

We encourage you to fork this guide and change the rules to fit your team's style guide. Below, you may list some amendments to the style guide. This allows you to periodically update your style guide without having to deal with merge conflicts.

};