JavaScript Style Guide
★ 148,017⑂ 26,517👁 3,641⑃ 1,726● 1,417▤ 0◈ 0
148,017
stars
+-2+-15+-72
26,517
forks
3,641
watchers
1,726
prs
1,417
issues
0
releases
0
discussions
README
Airbnb JavaScript Style Guide() {
A mostly reasonable approach to JavaScript
Note: this guide assumes you are using Babel, and requires that you use babel-preset-airbnb or the equivalent. It also assumes you are installing shims/polyfills in your app, with airbnb-browser-shims or the equivalent.
This guide is available in other languages too. See Translation
Other Style Guides
- ES5 (Deprecated)
- React
- CSS-in-JavaScript
- CSS & Sass
- Ruby
Table of Contents
1. Types
1. References
1. Objects
1. Arrays
1. Destructuring
1. Strings
1. Functions
1. Arrow Functions
1. Classes & Constructors
1. Modules
1. Iterators and Generators
1. Properties
1. Variables
1. Hoisting
1. Comparison Operators & Equality
1. Blocks
1. Control Statements
1. Comments
1. Whitespace
1. Commas
1. Semicolons
1. Type Casting & Coercion
1. Naming Conventions
1. Accessors
1. Events
1. jQuery
1. ECMAScript 5 Compatibility
1. ECMAScript 6+ (ES 2015+) Styles
1. Standard Library
1. Testing
1. Performance
1. Resources
1. In the Wild
1. Translation
1. The JavaScript Style Guide Guide
1. Chat With Us About JavaScript
1. Contributors
1. License
1. Amendments
Types
- 1.1 Primitives: When you access a primitive type you work directly on its value.
- string
- number
- boolean
- null
- undefined
- symbol
- bigint
javascript
const foo = 1;
let bar = foo;
bar = 9;
console.log(foo, bar); // = 1, 9
- Symbols and BigInts cannot be faithfully polyfilled, so they should not be used when targeting browsers/environments that don’t support them natively.
- 1.2 Complex: When you access a complex type you work on a reference to its value.
- object
- array
- function
javascript
const foo = [1, 2];
const bar = foo;
bar[0] = 9;
console.log(foo[0], bar[0]); // = 9, 9
⬆ back to top
References
- 2.1 Use const for all of your references; avoid using var. eslint: prefer-const, no-const-assign
Why? This ensures that you can’t reassign your references, which can lead to bugs and difficult to comprehend code.
javascript
// bad
var a = 1;
var b = 2;
// good
const a = 1;
const b = 2;
- 2.2 If you must reassign references, use let instead of var. eslint: no-var
Why? let is block-scoped rather than function-scoped like var.
javascript
// bad
var count = 1;
if (true) {
count += 1;
}
// good, use the let.
let count = 1;
if (true) {
count += 1;
}
- 2.3 Note that both let and const are block-scoped, whereas var is function-scoped.
javascript
// const and let only exist in the blocks they are defined in.
{
let a = 1;
const b = 1;
var c = 1;
}
console.log(a); // ReferenceError
console.log(b); // ReferenceError
console.log(c); // Top issues
#1089What rules do the community tend to override?151 comments · 2016-09-25#2804Support of new eslint flat config58 comments · 2023-08-06#216Best practices for promises?49 comments · 2014-11-18#2961eslint v9 support41 comments · 2024-04-06#60Say something about undefined26 comments · 2013-03-13#3075New releases for updated peer dependency on eslint-plugin-react22 comments · 2025-01-09#761single return vs multiple returns22 comments · 2016-02-23#2700Unexpected end of JSON input21 comments · 2022-12-27
相关报道 (5)
News & coverage (14)
Hacker NewsAirbnb JavaScript Style Guide▲ 303 · 150 comments · 2015-07-03Hacker NewsGreat React style guide by Airbnb▲ 50 · 19 comments · 2018-01-12Hacker NewsRuining the diversity of JavaScript community with a coding style guide▲ 50 · 26 comments · 2013-11-11Hacker NewsAirbnb's JavaScript style guide▲ 7 · 0 comments · 2013-01-12Hacker NewsAirbnb JavaScript Style Guide – A Mostly Reasonable Approach to JavaScript▲ 5 · 0 comments · 2015-03-27Hacker NewsJavaScript Style Guide by Airbnb▲ 4 · 0 comments · 2015-04-04HackerNoonI Tested REA, GitHub’s Viral Reverse Engineering Tool, on a Real Electron App2026-10-12githubNode 20 is no longer available in GitHub Actions2026-09-23learnGitHub Copilot modernization for JavaScript/TypeScript developers2026-07-30The GitHub BlogPrioritize Dependabot alerts with reachability analysis2026-10-28