Encodera

Thanks for reading!

Donate to the site — we work hard for you.

JavaScript reference

Nullish coalescing (??)

Returns the right-hand value if the left is null or undefined.

Available since: ES2020

Syntax

value ?? defaultValue

Examples

A default value:

const name = null;
 
console.log(name ?? 'Guest');

Output:

Guest

The difference from || — 0 and empty strings are kept:

const count = 0;
 
console.log(count ?? 10);
console.log(count || 10);

Output:

0
10

Notes

Use ?? for defaults when 0, '' and false are valid values. The || operator replaces any falsy value.

See also

Related recipes

In the tutorial

  • Logical operators — JavaScript logical operators — AND, OR, NOT, their precedence, default values with || and the ?? operator.

Thanks for reading!

Donate to the site — we work hard for you.