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).

Answer from DMeechan on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › Nullish_coalescing
Nullish coalescing operator (??) - JavaScript | MDN
The latter returns the right-hand side operand if the left operand is any falsy value, not only null or undefined. In other words, if you use || to provide some default value to another variable foo, you may encounter unexpected behaviors if you consider some falsy values as usable (e.g., '' or 0).
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'
🌐
JavaScript.info
javascript.info › tutorial › the javascript language › javascript fundamentals
Nullish coalescing operator '??'
The nullish coalescing operator ?? provides a short way to choose the first “defined” value from a list. It’s used to assign default values to variables: // set height=100, if height is null or undefined height = height ??
🌐
Reddit
reddit.com › r/typescript › nullish coalescing operator vs logical or
r/typescript on Reddit: Nullish Coalescing Operator vs Logical OR
July 13, 2022 -

Take this interface for example

interface Item {
  value?: number
}

Which among the two do you prefer if you will be accessing the value property of an item and why?

console.log(item.value ?? 1.00) // 1.00 is the fallback value
console.log(item.value || 1.00) // 1.00 is the fallback value

Personally, I prefer to use the nullish coalescing operator ?? for fallback values since it's more explicit and it sames me from the weird falsy values that JavaScript has.

🌐
DEV Community
dev.to › findniya › the-or-operator-vs-nullish-coalescing--agp
The OR operator (||) vs Nullish Coalescing (??) - DEV Community
April 25, 2023 - One of the key differences between ... values. The OR operator considers any falsy value (e.g. 0, '', false) to be equivalent to false, while the nullish coalescing operator only considers null and undefined to be nullish...
🌐
DEV Community
dev.to › justanordinaryperson › -nullish-coalescing-vs-logical-or-in-javascript-2l88
?? (Nullish coalescing) vs || (Logical OR) in Javascript - DEV Community
June 16, 2024 - Both Nullish Coalescing Operator (??) and Logical OR (||) operator are often used in JavaScript to provide a default value to variables.
🌐
Atomizedobjects
atomizedobjects.com › blog › javascript › nullish-coalescing-vs-or-javascript-operators
Nullish Coalescing vs OR - JavaScript operators (?? vs ||) | Atomized Objects
The difference between the Nullish Coalescing Operator vs OR Operator is that the Nullish Coalescing Operator checks the left hand argument for undefined or null (nullish), whereas the Logical OR Operator just checks the left hand argument to see if it is “falsy” or not.
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
Nullish coalescing operator VS OR operator
June 16, 2022 - please guys, is there any advantage of || over ?? . I see that ?? solves some of the problems that || can not solve
Find elsewhere
🌐
Clubmate
clubmate.fi › logical-operators
JavaScript logical operators and the new nullish coalescing operator – clubmate.fi
The nullish coalescing operator is kind of like OR, but it treats '', 0, and NaN as non-falsy, that’s essentially the only difference between OR and nullish coalescing.
🌐
Sal Ferrarello
salferrarello.com › home › javascript or (||) versus nullish coalescing operator (??)
JavaScript Or (||) Versus Nullish Coalescing Operator (??) - Sal Ferrarello
November 3, 2022 - When dealing with strings you’re typically better off using “or” (||) and for numbers you’re typically better off using the “nullish coalescing operator” (??).
🌐
Softwareshorts
softwareshorts.com › nullish-coalescing-vs-or-javascript-operators
Nullish Coalescing vs OR - JavaScript operators (?? vs ||) | SoftwareShorts
The main difference between the Nullish Coalescing Operator (??) and the Logical OR operator in JavaScript is that the nullish coalescing operator will fallback to the right hand argument only when the left hand argument is either null or undefined (Nullish).
🌐
Medium
medium.com › @gabrielairiart.gi › advanced-javascript-use-of-nullish-coalescing-and-optional-chaining-and-for-efficient-coding-7d1d3fe3eedf
Advanced JavaScript: Use of Nullish Coalescing ?? and Optional Chaining and ?. for Efficient Coding | by Gabriela Iriart | Medium
March 22, 2024 - Use ?. when you aim to safely access a property or method of an object that might not be defined. When juxtaposing Nullish Coalescing with Optional Chaining, it’s crucial to grasp that they fulfill distinct yet complementary roles. Nullish Coalescing ensures a variable is assigned a definitive value if it’s found to be null or undefined, thereby preventing the variable from remaining in an indeterminate state.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › Nullish_coalescing_assignment
Nullish coalescing assignment (??=) - JavaScript | MDN
No assignment is performed if the left-hand side is not nullish, due to short-circuiting of the nullish coalescing operator. For example, the following does not throw an error, despite x being const: ... const x = { get value() { return 1; }, set value(v) { console.log("Setter called"); }, }; x.value ??= 2; In fact, if x is not nullish, y is not evaluated at all.
🌐
DEV Community
dev.to › ibrahimbagalwa › when-to-use-or-and-nullish-coalescing-operator-39a2
When to use OR and Nullish coalescing Operator. - DEV Community
February 27, 2023 - In this example value is an empty string and empty string is a falsy value, || provides the fallback value "default value" However, ?? only considers null undefined as nullish, so it returns the value of value variable which is an empty string.
🌐
freeCodeCamp
freecodecamp.org › news › what-is-the-nullish-coalescing-operator-in-javascript-and-how-is-it-useful
What is the Nullish Coalescing Operator in JavaScript, and how is it useful
May 5, 2023 - By Dillion Megida The Nullish Coalescing Operator is a new logical operator in JavaScript introduced in ES 2020. In this article, we'll understand how this operator works. There are over four logical operators in JavaScript: the AND &&, OR ||, NOT !,...
🌐
MeasureThat
measurethat.net › Benchmarks › Show › 8712 › 0 › nullish-coalescing-vs-logical-or
Benchmark: Nullish coalescing (??) vs logical OR (||) - MeasureThat.net
JavaScript benchmarks, JavaScript performance playground. Measure performance accross different browsers. javascript benchmarks online.
🌐
freeCodeCamp
freecodecamp.org › news › javascript-advanced-operators
Advanced JavaScript Operators – Nullish Coalescing, Optional Chaining, and Destructuring Assignment
January 4, 2024 - As you can see, you don’t need an if-else statement to check for null or undefined values. The nullish coalescing operator was created as an improved alternative to the OR operator ||.