Comparison operators & equality
Use strict equality, understand truthy/falsy coercion, and avoid unnecessary complexity in conditions.15.1 Use === and !==
eslint: eqeqeq
Use === and !== over == and !=.
15.2 How conditional coercion works
Conditional statements such as theif statement evaluate their expression using coercion with the ToBoolean abstract method and always follow these simple rules:
- Objects evaluate to true
- Undefined evaluates to false
- Null evaluates to false
- Booleans evaluate to the value of the boolean
- Numbers evaluate to false if +0, -0, or NaN, otherwise true
- Strings evaluate to false if an empty string
'', otherwise true
An empty array
[] is an object and evaluates to true. This surprises many developers who expect it to be falsy like an empty string.15.3 Use shortcuts for booleans, explicit comparisons for strings and numbers
- Good
- Bad
15.5 Use braces for case clauses with lexical declarations
eslint: no-case-declarations
Use braces to create blocks in case and default clauses that contain lexical declarations (e.g. let, const, function, and class).
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.
- Good
- Bad
15.6 Don’t nest ternaries
eslint: no-nested-ternary
Ternaries should not be nested and generally be single line expressions.
- Good
- Bad
15.7 Avoid unneeded ternary statements
eslint: no-unneeded-ternary
- Good
- Bad
15.8 Enclose mixed operators in parentheses
eslint: no-mixed-operators
When mixing operators, enclose them in parentheses. The only exception is the standard arithmetic operators +, -, and ** since their precedence is broadly understood. We recommend enclosing / and * in parentheses because their precedence can be ambiguous when they are mixed.
This improves readability and clarifies the developer’s intention.
- Good
- Bad
15.9 Use the nullish coalescing operator ?? precisely
The nullish coalescing operator (??) returns its right-hand side operand when its left-hand side operand is null or undefined. Otherwise, it returns the left-hand side operand.
It provides precision by distinguishing null/undefined from other falsy values, enhancing code clarity and predictability.
- Good
- Bad
Blocks
Consistent block formatting prevents ambiguity in multi-statement control flow.16.1 Use braces with all multiline blocks
eslint: nonblock-statement-body-position
- Good
- Bad
16.2 Put else on the same line as the closing brace
eslint: brace-style
If you’re using multiline blocks with if and else, put else on the same line as your if block’s closing brace.
- Good
- Bad
16.3 Omit else after a returning if block
eslint: no-else-return
If an if block always executes a return statement, the subsequent else block is unnecessary. A return in an else if block following an if block that contains a return can be separated into multiple if blocks.
- Good
- Bad
Control statements
Keep condition lines readable and useif statements instead of short-circuit operators for side effects.
17.1 Break long conditions onto new lines
In case your control statement (if, while etc.) gets too long or exceeds the maximum line length, each (grouped) condition can be put on a new line. The logical operator should begin the line.
Requiring operators at the beginning of the line keeps the operators aligned and follows a pattern similar to method chaining. This also improves readability by making it easier to visually follow complex logic.
- Good
- Bad
17.2 Don’t use selection operators in place of control statements
- Good
- Bad