This rule enforces the use of nullish coalescing operator (??) over logical OR (||) when dealing with null or undefined values, particularly when the right-hand side is a falsy but not nullish value.
- const displayName = user.name || ''
+ const displayName = user.name ?? ''
- const message = input || ''
+ const message = input ?? ''
- const title = config.title || ''
+ const title = config.title ?? ''- const count = items.length || 0
+ const count = items.length ?? 0
- const index = searchIndex || 0
+ const index = searchIndex ?? 0
- const timeout = settings.timeout || 0
+ const timeout = settings.timeout ?? 0+ const enabled = feature.flag ?? false
- const enabled = feature.flag || false
- const visible = config.show || false
+ const visible = config.show ?? false
- const required = field.required || false
+ const required = field.required ?? false const user = {
- name: input.name || '',
+ name: input.name ?? '',
- count: data.count || 0,
+ count: data.count ?? 0,
- active: status.active || false
+ active: status.active ?? false
}- const greeting = `Hello ${user.name || ''}!`
+ const greeting = `Hello ${user.name ?? ''}!`
- const status = `Count: ${items.length || 0}`
+ const status = `Count: ${items.length ?? 0}`
- const message = `Debug: ${config.debug || false}`
+ const message = `Debug: ${config.debug ?? false}`This rule only applies to:
- ✅ Logical OR (
||) operators where the right-hand side is a falsy but not nullish value:- Empty string (
"") - Zero (
0) - Boolean
false
- Empty string (
This rule does NOT apply to:
- ❌ Logical OR (
||) with non-falsy values ("hello",1,true) - ❌ Logical AND (
&&) operators - ❌ Nullish coalescing (
??) operators - ❌ Other logical operators (
!,&&,??)
The rule provides auto-fix suggestions that replace || with ??:
value || ""→value ?? ""count || 0→count ?? 0flag || false→flag ?? false(user?.name || "") || false→(user?.name ?? "") || false(first occurrence)