Skip to content

useNullishCoalescing (JavaScript)

Language JavaScript (and super languages)
biome.json
{
"linter": {
"rules": {
"nursery": {
"useNullishCoalescing": "error"
}
}
}
}

Enforce using the nullish coalescing operator (??) instead of logical or (||).

?? only checks for null and undefined, while || checks for any falsy value including 0, '', and false. The rule reports || and ||= when type analysis shows the left operand is possibly nullish, and ternary expressions that compare a value with null or undefined to pick a fallback. A strict comparison with only one of null and undefined is reported only when the value cannot hold the other one.

invalid-or.ts
declare const maybeString: string | null;
const value = maybeString || 'default';
/invalid-or.ts:2:27 lint/nursery/useNullishCoalescing ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ Use ?? instead of ||.

1 │ declare const maybeString: string | null;
> 2 │ const value = maybeString || 'default';
│ ^^
3 │

ℹ The || operator checks for all falsy values (including 0, '', and false), while ?? only checks for null and undefined.

ℹ This rule is still being actively worked on, so it may be missing features or have rough edges. Visit https://github.com/biomejs/biome/issues/8043 for more information or to report possible bugs.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

invalid-or-undefined.ts
declare const maybeNumber: number | undefined;
const value = maybeNumber || 0;
/invalid-or-undefined.ts:2:27 lint/nursery/useNullishCoalescing ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ Use ?? instead of ||.

1 │ declare const maybeNumber: number | undefined;
> 2 │ const value = maybeNumber || 0;
│ ^^
3 │

ℹ The || operator checks for all falsy values (including 0, '', and false), while ?? only checks for null and undefined.

ℹ This rule is still being actively worked on, so it may be missing features or have rough edges. Visit https://github.com/biomejs/biome/issues/8043 for more information or to report possible bugs.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

invalid-or-assign.ts
declare let x: string | null;
x ||= 'default';
/invalid-or-assign.ts:2:3 lint/nursery/useNullishCoalescing ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ Use ??= instead of ||=.

1 │ declare let x: string | null;
> 2 │ x ||= 'default';
│ ^^^
3 │

ℹ The ||= operator assigns when the left side is falsy, while ??= only assigns when it is null or undefined.

ℹ This rule is still being actively worked on, so it may be missing features or have rough edges. Visit https://github.com/biomejs/biome/issues/8043 for more information or to report possible bugs.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

invalid-ternary-not-null.ts
declare const x: string | null;
const value = x !== null ? x : 'default';
/invalid-ternary-not-null.ts:2:15 lint/nursery/useNullishCoalescing  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━

ℹ Prefer ?? over a ternary expression checking for nullish.

1 │ declare const x: string | null;
> 2 │ const value = x !== null ? x : 'default';
│ ^^^^^^^^^^
3 │

ℹ A ternary expression that checks for null or undefined does what ?? does, but ?? is shorter and easier to read.

ℹ This rule is still being actively worked on, so it may be missing features or have rough edges. Visit https://github.com/biomejs/biome/issues/8043 for more information or to report possible bugs.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

ℹ Safe fix: Replace the ternary with ??.

1 1 │ declare const x: string | null;
2 │ - const·value·=·x·!==·null·?·x·:·'default';
2 │ + const·value·=·x·??·'default';
3 3 │

invalid-ternary-loose-null.ts
declare const x: string | null;
const value = x == null ? 'default' : x;
/invalid-ternary-loose-null.ts:2:15 lint/nursery/useNullishCoalescing  FIXABLE  ━━━━━━━━━━━━━━━━━━━━

ℹ Prefer ?? over a ternary expression checking for nullish.

1 │ declare const x: string | null;
> 2 │ const value = x == null ? 'default' : x;
│ ^^^^^^^^^
3 │

ℹ A ternary expression that checks for null or undefined does what ?? does, but ?? is shorter and easier to read.

ℹ This rule is still being actively worked on, so it may be missing features or have rough edges. Visit https://github.com/biomejs/biome/issues/8043 for more information or to report possible bugs.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

ℹ Safe fix: Replace the ternary with ??.

1 1 │ declare const x: string | null;
2 │ - const·value·=·x·==·null·?·'default'·:·x;
2 │ + const·value·=·x·??·'default';
3 3 │

An if statement that only assigns to a nullish variable is also reported, since it can be rewritten as ??=.

invalid-if-assignment.ts
declare let a: { x: string } | null;
declare function makeA(): { x: string };
if (!a) {
a = makeA();
}
/invalid-if-assignment.ts:3:1 lint/nursery/useNullishCoalescing ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ Use ??= instead of an if statement for nullish assignment.

1 │ declare let a: { x: string } | null;
2 │ declare function makeA(): { x: string };
> 3 │ if (!a) {
│ ^^^^^^^^^
> 4 │ a = makeA();
> 5 │ }
│ ^
6 │

ℹ This if statement only assigns when the variable is nullish, which ??= expresses directly.

ℹ This rule is still being actively worked on, so it may be missing features or have rough edges. Visit https://github.com/biomejs/biome/issues/8043 for more information or to report possible bugs.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

valid-nullish-coalescing.ts
declare const maybeString: string | null;
const value = maybeString ?? 'default';
valid-non-nullish.ts
declare const definiteString: string;
const value = definiteString || 'fallback';
valid-nullish-assign.ts
declare let y: string | null;
y ??= 'default';

By default, Biome ignores || expressions inside the test of an if, while, for, or do-while statement or of a ternary expression. Set this to false to report them. Default: true.

biome.json
{
"linter": {
"rules": {
"nursery": {
"useNullishCoalescing": {
"level": "on",
"options": {
"ignoreConditionalTests": false
}
}
}
}
}
}
invalid-conditional-tests.ts
declare const cond: string | null;
if (cond || 'fallback') {}
/invalid-conditional-tests.ts:2:10 lint/nursery/useNullishCoalescing ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ Use ?? instead of ||.

1 │ declare const cond: string | null;
> 2 │ if (cond || 'fallback') {}
│ ^^
3 │

ℹ The || operator checks for all falsy values (including 0, '', and false), while ?? only checks for null and undefined.

ℹ This rule is still being actively worked on, so it may be missing features or have rough edges. Visit https://github.com/biomejs/biome/issues/8043 for more information or to report possible bugs.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

With the default value, || in a conditional test is not reported.

valid-conditional-tests.ts
declare const cond: string | null;
while (cond || 'fallback') {}

By default, Biome reports ternary expressions that compare a value with null or undefined and can be replaced with ??. Set this to true to ignore them. Default: false.

biome.json
{
"linter": {
"rules": {
"nursery": {
"useNullishCoalescing": {
"level": "on",
"options": {
"ignoreTernaryTests": true
}
}
}
}
}
}

|| is still reported when ternary expressions are ignored.

invalid-ternary-tests.ts
declare const maybeString: string | null;
const value = maybeString || 'default';
/invalid-ternary-tests.ts:2:27 lint/nursery/useNullishCoalescing ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ Use ?? instead of ||.

1 │ declare const maybeString: string | null;
> 2 │ const value = maybeString || 'default';
│ ^^
3 │

ℹ The || operator checks for all falsy values (including 0, '', and false), while ?? only checks for null and undefined.

ℹ This rule is still being actively worked on, so it may be missing features or have rough edges. Visit https://github.com/biomejs/biome/issues/8043 for more information or to report possible bugs.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

valid-ternary-tests.ts
declare const x: string | null;
const value = x !== null ? x : 'default';

By default, Biome reports || and ||= even when they are combined with &&, as in (a || b) && c or a || (b && c). Set this to true to ignore || and ||= whose connected logical tree also contains a &&. Default: false.

biome.json
{
"linter": {
"rules": {
"nursery": {
"useNullishCoalescing": {
"level": "on",
"options": {
"ignoreMixedLogicalExpressions": true
}
}
}
}
}
}

|| and ||= are still reported when the surrounding logical tree does not contain &&.

invalid-mixed-or.ts
declare const maybeString: string | null;
const value = maybeString || 'default';
/invalid-mixed-or.ts:2:27 lint/nursery/useNullishCoalescing ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ Use ?? instead of ||.

1 │ declare const maybeString: string | null;
> 2 │ const value = maybeString || 'default';
│ ^^
3 │

ℹ The || operator checks for all falsy values (including 0, '', and false), while ?? only checks for null and undefined.

ℹ This rule is still being actively worked on, so it may be missing features or have rough edges. Visit https://github.com/biomejs/biome/issues/8043 for more information or to report possible bugs.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

invalid-mixed-or-assign.ts
declare let assigned: string | null;
assigned ||= 'default';
/invalid-mixed-or-assign.ts:2:10 lint/nursery/useNullishCoalescing ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ Use ??= instead of ||=.

1 │ declare let assigned: string | null;
> 2 │ assigned ||= 'default';
│ ^^^
3 │

ℹ The ||= operator assigns when the left side is falsy, while ??= only assigns when it is null or undefined.

ℹ This rule is still being actively worked on, so it may be missing features or have rough edges. Visit https://github.com/biomejs/biome/issues/8043 for more information or to report possible bugs.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

|| and ||= mixed with && in the same logical tree are not reported.

valid-mixed-or.ts
declare const a: string | null;
declare const b: string;
const r = (a || 'default') && b;
valid-mixed-or-assign.ts
declare const b: string;
declare let assigned: string | null;
assigned ||= b && 'fallback';

By default, Biome reports || and ||= even when they are the argument of a call to the global Boolean() function. Set this to true to ignore them there, where coalescing on falsy values is intentional. Default: false.

biome.json
{
"linter": {
"rules": {
"nursery": {
"useNullishCoalescing": {
"level": "on",
"options": {
"ignoreBooleanCoercion": true
}
}
}
}
}
}

|| and ||= that are not the argument of a Boolean() call are still reported.

invalid-boolean-coercion.ts
declare const maybeString: string | null;
const value = maybeString || 'default';
/invalid-boolean-coercion.ts:2:27 lint/nursery/useNullishCoalescing ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ Use ?? instead of ||.

1 │ declare const maybeString: string | null;
> 2 │ const value = maybeString || 'default';
│ ^^
3 │

ℹ The || operator checks for all falsy values (including 0, '', and false), while ?? only checks for null and undefined.

ℹ This rule is still being actively worked on, so it may be missing features or have rough edges. Visit https://github.com/biomejs/biome/issues/8043 for more information or to report possible bugs.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

|| and ||= that are the argument of a Boolean() call are not reported. The argument may be parenthesized or combined with other || and && operators.

valid-boolean-coercion.ts
declare const a: string | null;
declare const b: string;
const r = Boolean(a || b);

By default, Biome reports ||, ||=, ternary expressions, and if statements whatever primitive types their operand includes. Set this to true to ignore them when the operand is a nullable union whose non-nullish variants are all string, number, boolean, or bigint, or to an object with any of the keys string, number, boolean, and bigint set to true to ignore only unions made of those kinds. Other types, such as a symbol | null union or a non-union string, are still reported. Default: none.

biome.json
{
"linter": {
"rules": {
"nursery": {
"useNullishCoalescing": {
"level": "on",
"options": {
"ignorePrimitives": {
"string": true
}
}
}
}
}
}
}

Primitive kinds that are not opted out of are still reported.

invalid-primitives.ts
declare const count: number | null;
const value = count || 0;
/invalid-primitives.ts:2:21 lint/nursery/useNullishCoalescing ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ Use ?? instead of ||.

1 │ declare const count: number | null;
> 2 │ const value = count || 0;
│ ^^
3 │

ℹ The || operator checks for all falsy values (including 0, '', and false), while ?? only checks for null and undefined.

ℹ This rule is still being actively worked on, so it may be missing features or have rough edges. Visit https://github.com/biomejs/biome/issues/8043 for more information or to report possible bugs.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

A string operand is not reported when string is ignored.

valid-primitives.ts
declare const name: string | null;
const value = name || 'default';

By default, Biome reports an if statement that only assigns to a nullish variable, since it can be rewritten as ??=. Set this to true to ignore those statements. Default: false.

biome.json
{
"linter": {
"rules": {
"nursery": {
"useNullishCoalescing": {
"level": "on",
"options": {
"ignoreIfStatements": true
}
}
}
}
}
}

|| and ||= are still reported when only if statements are ignored.

invalid-if-statements.ts
declare const maybeString: string | null;
const value = maybeString || 'default';
/invalid-if-statements.ts:2:27 lint/nursery/useNullishCoalescing ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ Use ?? instead of ||.

1 │ declare const maybeString: string | null;
> 2 │ const value = maybeString || 'default';
│ ^^
3 │

ℹ The || operator checks for all falsy values (including 0, '', and false), while ?? only checks for null and undefined.

ℹ This rule is still being actively worked on, so it may be missing features or have rough edges. Visit https://github.com/biomejs/biome/issues/8043 for more information or to report possible bugs.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

An if statement performing a nullish assignment is not reported.

valid-if-statements.ts
declare let a: { x: string } | null;
declare function makeA(): { x: string };
if (!a) {
a = makeA();
}