Skip to content

assignmentOperatorShorthands

Prefer logical assignment operator shorthand expressions.

✅ This rule is included in the ts stylistic preset.

ES2021 introduced logical assignment operators (||=, &&=, ??=) that combine logical operations with assignment. These shorthand operators make code more concise and express intent more clearly.

For example, a = a || b can be written as a ||= b. This pattern is common for providing default values or conditionally updating variables.

let value = 0;
value = value || 1;
let config: Config | undefined;
config = config ?? getDefaultConfig();
let enabled = false;
enabled = enabled && isFeatureAvailable();

This rule is not configurable.

If you need to support environments that do not have ES2021 support, you may need to avoid using logical assignment operators. You may also prefer the more explicit expanded form if you find it more readable.

Made with ❤️‍🔥 in Boston by Josh Goldberg and contributors.