🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › Nullish_coalescing
Nullish coalescing operator (??) - JavaScript | MDN
The nullish coalescing (??) operator is a logical operator that returns its right-hand side operand when its left-hand side operand is null or undefined, and otherwise returns its left-hand side operand.
🌐
JavaScript.info
javascript.info › tutorial › the javascript language › javascript fundamentals
Nullish coalescing operator '??'
April 23, 2023 - The precedence of the ?? operator is the same as ||. They both equal 3 in the MDN table. That means that, just like ||, the nullish coalescing operator ??
Discussions

TIL about the Nullish Coalescing Operator in Javascript (??)
Wait until you learn about the elvis operator More on reddit.com
🌐 r/ProgrammerTIL
18
78
March 30, 2022
When should I use ?? (nullish coalescing) vs || (logical OR)?
Related to Is there a "null coalescing" operator in JavaScript? - JavaScript now has a ?? operator which I see in use more frequently. Previously most JavaScript code used ||. let userAge = null // These values will be the same. let age1 = userAge || 21 let age2 = userAge ?? 21 · In what circumstances will ?? and || behave differently? ... The OR operator || uses the right value if left is falsy, while the nullish ... More on stackoverflow.com
🌐 stackoverflow.com
Nullish Coalescing Operator vs Logical OR
They are different, if you want value=0 to fall back to 1 you should use || otherwise use ?? More on reddit.com
🌐 r/typescript
4
2
July 13, 2022
Why doesn't JavaScript have a nullish-coalescing-assignment (??=) operator?
Part of ES2021: https://github.com/tc39/proposal-logical-assignment More on reddit.com
🌐 r/javascript
7
2
July 31, 2021
🌐
W3Schools
w3schools.com › jsref › jsref_oper_nullish.asp
W3Schools.com
The ?? operator returns the right operand when the left operand is nullish (null or undefined), otherwise it returns the left operand.
🌐
Reddit
reddit.com › r/programmertil › til about the nullish coalescing operator in javascript (??)
r/ProgrammerTIL on Reddit: TIL about the Nullish Coalescing Operator in Javascript (??)
March 30, 2022 -

Often if you want to say, "x, if it exists, or y", you'd use the or operator ||.

Example:

const foo = bar || 3;

However, let's say you want to check that the value of foo exists. If foo is 0, then it would evaluate to false, and the above code doesn't work.

So instead, you can use the Nullish Coalescing Operator.

const foo = bar ?? 3;

This would evaluate to 3 if bar is undefined or null, and use the value of bar otherwise.

In typescript, this is useful for setting default values from a nullable object.

setFoo(foo: Foo|null) {
  this.foo = foo ?? DEFAULT_FOO;
}
Top answer
1 of 9
993

The OR operator || uses the right value if left is falsy, while the nullish coalescing operator ?? uses the right value if left is null or undefined.

These operators are often used to provide a default value if the first one is missing.

But the OR operator || can be problematic if your left value might contain "" or 0 or false (because these are falsy values):

console.log(12 || "not found") // 12
console.log(0  || "not found") // "not found"

console.log("jane" || "not found") // "jane"
console.log(""     || "not found") // "not found"

console.log(true  || "not found") // true
console.log(false || "not found") // "not found"

console.log(undefined || "not found") // "not found"
console.log(null      || "not found") // "not found"

In many cases, you might only want the right value if left is null or undefined. That's what the nullish coalescing operator ?? is for:

console.log(12 ?? "not found") // 12
console.log(0  ?? "not found") // 0

console.log("jane" ?? "not found") // "jane"
console.log(""     ?? "not found") // ""

console.log(true  ?? "not found") // true
console.log(false ?? "not found") // false

console.log(undefined ?? "not found") // "not found"
console.log(null      ?? "not found") // "not found"

While the ?? operator isn't available in current LTS versions of Node (v10 and v12), you can use it with some versions of TypeScript or Node:

The ?? operator was added to TypeScript 3.7 back in November 2019.

And more recently, the ?? operator was included in ES2020, which is supported by Node 14 (released in April 2020).

When the nullish coalescing operator ?? is supported, I typically use it instead of the OR operator || (unless there's a good reason not to).

2 of 9
403

In short

The Nullish Coalescing Operator ?? distinguishes between:

  • nullish values (null, undefined)
  • falsey but defined values (false, 0, '' etc.)

|| (logical OR) treats both of these the same.

I created a simple graphic to illustrate the relationship of nullish and falsey values in JavaScript:

Further explanation:

let x, y

x = 0
y = x || 'default'            // y = 'default'
y = x ?? 'default'            // y = 0

As seen above, the difference between the operators ?? and || is that one is checking for nullish values and one is checking for falsey values. However, there are many instances where they behave the same. That is because in JavaScript every nullish value is also falsey (but not every falsey value is nullish).

Using what we learned above we can create a few examples for different behavior:

let y

y = false || 'default'       // y = 'default'
y = false ?? 'default'       // y = false

y = 0n || 'default'          // y = 'default'
y = 0n ?? 'default'          // y = 0n

y = NaN || 'default'         // y = 'default'
y = NaN ?? 'default'         // y = NaN

y = '' || 'default'          // y = 'default'
y = '' ?? 'default'          // y = ''

Since the new Nullish Coalescing Operator can differentiate between no value and a falsey value, it can be beneficial if you, for example, need to check if there is no string or an empty string. Generally, you probably want to use ?? instead of || most of the time.

Last and also least here are the two instances where they behave the same:

let y

y = null || 'default'        // y = 'default'
y = null ?? 'default'        // y = 'default'

y = undefined || 'default'   // y = 'default'
y = undefined ?? 'default'   // y = 'default'
🌐
TypeScript ESlint
typescript-eslint.io › rules › prefer-nullish-coalescing
prefer-nullish-coalescing | typescript-eslint
Because the nullish coalescing operator only coalesces when the original value is null or undefined, it is much safer than relying upon logical OR operator chaining ||, which coalesces on any falsy value.
Find elsewhere
🌐
Wikipedia
en.wikipedia.org › wiki › Null_coalescing_operator
Null coalescing operator - Wikipedia
October 31, 2025 - It evaluates its left-hand operand and, if the result value is not "nullish" (null or undefined), takes that value as its result; otherwise, it evaluates the right-hand operand and takes the resulting value as its result. In the following example, a will be assigned the value of b if the value of b is not null or undefined, otherwise it will be assigned 3. ... Before the nullish coalescing operator, programmers would use the logical OR operator (||).
🌐
TypeScript
typescriptlang.org › play › 3-7 › syntax-and-messaging › nullish-coalescing.ts.html
TypeScript: Playground Example - Nullish Coalescing
The nullish coalescing operator is an alternative to || which returns the right-side expression if the left-side is null or undefined. In contrast, || uses falsy checks, meaning an empty string or the number 0 would be considered false.
🌐
V8
v8.dev › features › nullish-coalescing
Nullish coalescing · V8
September 17, 2019 - The nullish coalescing operator (??) acts very similar to the || operator, except that we don’t use “truthy” when evaluating the operator. Instead we use the definition of “nullish”, meaning “is the value strictly equal to null or undefined”. So imagine the expression lhs ??
🌐
daily.dev
daily.dev › home › blog › webdev › nullish coalescing operator (??) in javascript - what is it and how to use it?
Nullish Coalescing Operator (??) In JavaScript - What Is It And How To Use It?
November 1, 2021 - The Nullish Coalescing Operator allows us to check if a value is null or undefined, and provide a fallback value if that is the case.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › Nullish_coalescing_assignment
Nullish coalescing assignment (??=) - JavaScript | MDN
The nullish coalescing assignment (??=) operator, also known as the logical nullish assignment operator, only evaluates the right operand and assigns to the left if the left operand is nullish (null or undefined).
🌐
Alexanderkaran
blog.alexanderkaran.com › nullish-coalescing-operator
Nullish Coalescing Operator
August 17, 2023 - If the user has set the volume ... // 0 · The nullish coalescing operator is a great way to provide a default value when safely unwrapping or accessing values in JavaScript....
🌐
Babel
babeljs.io › presets › @babel/preset-env › nullish-coalescing-operator
@babel/plugin-transform-nullish-coalescing-operator · Babel
We cannot use != null here because document.all == null and document.all has been deemed not "nullish".
🌐
Nicotsou
nicotsou.com › tltr-typescript-nulish-coalescing
A Guide To Understand The Nullish Coalescing (??) Operator
December 2, 2022 - Nullish Coalescing allows you to “fall back” to a default value when dealing with the nullable values.
🌐
DEV Community
dev.to › blamb31 › nullish-coalescing-operator-4amf
Nullish Coalescing Operator - DEV Community
December 2, 2022 - This meant that it would be evaluate to false on null, undefined, 0, '', NaN and false. With the Nullish Coalescing Operator (??), it will only evaluate to false if the value is null or undefined.
🌐
DEV Community
dev.to › laurieontech › nullish-coalescing-let-falsy-fool-you-no-more-41c0
Nullish Coalescing - Let Falsy Fool You No More - DEV Community
December 13, 2019 - There are a few different ways to do this, but one of the most common is to use or, represented by ||. let snippet = null let snippet = snippet || "code snippet" In this example, snippet is set to null.
🌐
Salesforce Developers
developer.salesforce.com › docs › atlas.en-us.apexcode.meta › apexcode › langCon_apex_NullCoalescingOperator.htm
Null Coalescing Operator | Apex Developer Guide | Salesforce Developers
The ?? operator returns its right-hand side operand when its left-hand side operand is null. Similar to the safe navigation operator (?.), the null coalescing operator (??) replaces verbose and explicit checks for null references in code.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-nullish-coalescing-operator
JavaScript Nullish Coalescing(??) Operator - GeeksforGeeks
The nullish coalescing (??) operator is used to handle null and undefined values in JavaScript.
Published   January 22, 2026
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › csharp › language-reference › operators › null-coalescing-operator
?? and ??= operators - null-coalescing operators - C# reference | Microsoft Learn
January 24, 2026 - The `??` and `??=` operators are the C# null-coalescing operators. They return the value of the left-hand operand if it isn't null. Otherwise, they return the value of the right-hand operand