Iterators and generators
Prefer functional iteration patterns over imperative loops, and avoid generators until they transpile reliably.11.1 Don’t use iterators
eslint: no-iterator, no-restricted-syntax
Prefer JavaScript’s higher-order functions instead of loops like for-in or for-of. This enforces immutability. Dealing with pure functions that return values is easier to reason about than side effects.
Use map() / every() / filter() / find() / findIndex() / reduce() / some() to iterate over arrays, and Object.keys() / Object.values() / Object.entries() to produce arrays so you can iterate over objects.
- Good
- Bad
11.2 Don’t use generators for now
Generators don’t transpile well to ES5.If your target environment natively supports generators, or you are not transpiling to ES5, this restriction may not apply. See rule 11.3 for proper formatting if you must use them.
11.3 Space generator function signatures properly
eslint: generator-star-spacing
If you must use generators, make sure their function signature is spaced properly. function and * are part of the same conceptual keyword — * is not a modifier for function. function* is a unique construct, different from function.
- Good
- Bad
Properties
Use dot notation by default, bracket notation for dynamic access, and** for exponentiation.
12.1 Use dot notation when accessing properties
eslint: dot-notation
- Good
- Bad
12.2 Use bracket notation for variable property access
Use bracket notation[] when accessing properties with a variable.
Bracket notation is necessary when the property name is dynamic — stored in a variable or computed at runtime.
12.3 Use the exponentiation operator **
eslint: prefer-exponentiation-operator
Use the exponentiation operator ** when calculating exponentiations instead of Math.pow.
- Good
- Bad