JavaScript Style Guide
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
1.1 Primitives: When you access a primitive type you work directly on its value.
stringnumberbooleannullundefinedsymbolbigint1.2 Complex: When you access a complex type you work on a reference to its value.
objectarrayfunction⬆ 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.
2.2 If you must reassign references, use let instead of var. eslint: no-var
Why?
letis block-scoped rather than function-scoped likevar.
2.3 Note that both let and const are block-scoped, whereas var is function-scoped.
In the above code, you can see that referencing a and b will produce a ReferenceError, while c contains the number. This is because a and b are block scoped, while c is scoped to the containing function.
⬆ back to top Objects
no-new-object3.2 Use computed property names when creating objects with dynamic property names.
Why? They allow you to define all the properties of an object in one place.
object-shorthand3.4 Use property value shorthand. eslint: object-shorthand
Why? It is shorter and descriptive.
3.5 Group your shorthand properties at the beginning of your object declaration.
Why? It’s easier to tell which properties are using the shorthand.
3.6 Only quote properties that are invalid identifiers. eslint: quote-props
Why? In general we consider it subjectively easier to read. It improves syntax highlighting, and is also more easily optimized by many JS engines.
3.7 Do not call Object.prototype methods directly, such as hasOwnProperty, propertyIsEnumerable, and isPrototypeOf. eslint: no-prototype-builtins
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)). In modern browsers that support ES2022, or with a polyfill such as <https:/
Severity: Unhandled promise rejection in `whitespace-async.js` when ESLint async path is used
Can we publish eslint-config-airbnb v19.0.5 please?
Inconsistent semicolon usage in examples (Arrays vs Functions)
Lint the readme
Support of new eslint flat config
Discussion: Arrow Functions vs Traditional Functions
No error handling around execSync + JSON.parse in whitespace.js (ESLint 9 path)
Upgrading eslint-plugin-react-hooks
What is the point of the "Types" section of the guide?
[eslint-config-airbnb] `jsx-a11y/control-has-associated-label` config does not ignore `td` and `th` HTML elements