JavaScript Style Guide
Relevant source files
Purpose and Scope
This document describes the core JavaScript style guide for the Airbnb JavaScript Style Guide repository, as documented in README.md1-4098 The guide defines fundamental language patterns, syntax conventions, and best practices for writing modern JavaScript (ES6+). It assumes the use of Babel transpilation via babel-preset-airbnb and browser polyfills via airbnb-browser-shims.
For details on specific topics, see the following child pages:
- Types, Variables, and References
- Objects, Arrays, and Destructuring
- Strings and Template Literals
- Functions and Arrow Functions
- Classes, Modules, and Iterators
- Comparison Operators and Control Flow
- Code Style and Formatting
Sources: README.md1-10
Style Guide Organization
The JavaScript style guide is organized into 30 major sections covering language fundamentals through testing and performance considerations. The core rules are enforced via ESLint configurations in eslint-config-airbnb and eslint-config-airbnb-base packages.
Language Architecture Overview
The following diagram maps high-level style categories to the specific code entities and rules that govern them.
Sources: README.md21-61 README.md63-3820
Core Language Fundamentals
Types and References
JavaScript distinguishes between primitive types (pass-by-value) and complex types (pass-by-reference). The guide mandates const for all references that are not reassigned and let for reassignable references, strictly prohibiting var.
For details, see Types, Variables, and References.
Sources: README.md63-162
Objects, Arrays, and Destructuring
The guide enforces literal syntax for objects and arrays, discouraging constructors. It promotes modern ES6+ features like computed properties, method shorthand, and the spread operator for immutability. Destructuring is required for accessing multiple properties.
For details, see Objects, Arrays, and Destructuring.
Sources: README.md164-576
Strings and Template Literals
Single quotes are the standard for strings. Template literals are required for interpolation or multi-line strings to avoid messy concatenation.
For details, see Strings and Template Literals.
Sources: README.md578-659
Execution and Structure
Functions and Arrow Functions
The guide prioritizes named function expressions over declarations to control hoisting. Arrow functions are required for anonymous callbacks and cases where lexical this is needed.
For details, see Functions and Arrow Functions.
Sources: README.md661-1107
Classes, Modules, and Iterators
ES6 class syntax is mandatory over direct prototype manipulation. The module system relies on import/export rather than CommonJS require. Functional iteration methods (like map and reduce) are preferred over for-in or for-of loops.
For details, see Classes, Modules, and Iterators.
Sources: README.md1109-1591
Logic and Formatting
Comparison and Control Flow
Strict equality (===) is required. The guide provides specific truthiness rules and dictates the structure of blocks, including "cuddled" else statements and mandatory braces for multi-line conditions.
For details, see Comparison Operators and Control Flow.
Sources: README.md2022-2419
Code Style and Formatting
This covers the "look and feel" of the code: 2-space indentation, 100-character line limits, mandatory semicolons, and trailing commas. It also defines naming conventions like camelCase for variables and PascalCase for classes.
For details, see Code Style and Formatting.
Sources: README.md2562-3575
ESLint Rule Integration
The style guide is implemented through a series of ESLint rules defined in the configuration packages.
Code Entity to ESLint Rule Mapping
The following diagram bridges natural language style requirements to the specific ESLint rules used in the codebase to enforce them.
Sources: packages/eslint-config-airbnb-base/rules/best-practices.js53-54 packages/eslint-config-airbnb-base/rules/style.js124-147 packages/eslint-config-airbnb-base/rules/style.js523-524
Standard Library and Testing
The guide extends to the usage of the JavaScript standard library and testing practices:
- Standard Library: Prefer
Number.isNaN()over the globalisNaN()andNumber.isFinite()over globalisFinite()README.md3781-3819 - Testing: Recommends small, pure functions to facilitate testing with frameworks like Jest or Mocha README.md3821-3841
Sources: README.md3781-3841
Refresh this wiki
On this page
- JavaScript Style Guide
- Purpose and Scope
- Style Guide Organization
- Language Architecture Overview
- Core Language Fundamentals
- Types and References
- Objects, Arrays, and Destructuring
- Strings and Template Literals
- Execution and Structure
- Functions and Arrow Functions
- Classes, Modules, and Iterators
- Logic and Formatting
- Comparison and Control Flow
- Code Style and Formatting
- ESLint Rule Integration
- Code Entity to ESLint Rule Mapping
- Standard Library and Testing
