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 '??'
Historically, the OR || operator was there first. It’s been there since the beginning of JavaScript, so developers were using it for such purposes for a long time. On the other hand, the nullish coalescing operator ??
🌐
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 - Logical OR (||): This is useful when you want a default value if the left operand is any falsy value. This includes null, undefined, 0, empty string (""), false, and NaN. ... If you want to check and avoid printing empty values, use the Logical ...
🌐
DEV Community
dev.to › findniya › the-or-operator-vs-nullish-coalescing--agp
The OR operator (||) vs Nullish Coalescing (??) - DEV Community
April 25, 2023 - In this case, the OR operator returns the second expression false because the first expression 0 is falsy. The nullish coalescing operator (??) returns the first defined value in a series of expressions.
🌐
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.

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.
🌐
Atomizedobjects
atomizedobjects.com › blog › javascript › nullish-coalescing-vs-or-javascript-operators
Nullish Coalescing vs OR - JavaScript operators (?? vs ||) | Atomized Objects
So, to summarize Nullish Coalescing Operator vs OR Operator, the Nullish Coalescing Operator checks for null or undefined values and the logical OR operator checks for falsy values. Will you be using the Nullish Coalescing Operator more now? ... More money in your pocket by the end of the month.
🌐
Stackademic
blog.stackademic.com › clarifying-javascripts-nullish-coalescing-operator-vs-logical-or-4851135fc586
Clarifying JavaScript’s Nullish Coalescing Operator (??) vs Logical OR (||) | by Chioma Nwocha | Stackademic
August 23, 2023 - By adding parentheses, you ensure that the nullish coalescing operations are performed before the multiplication, allowing you to calculate the BMI accurately even when some values are missing or null. JavaScript prohibits using ?? in conjunction with && and ||operators without explicit parentheses.
🌐
Sal Ferrarello
salferrarello.com › home › javascript or (||) versus nullish coalescing operator (??)
JavaScript Or (||) Versus Nullish Coalescing Operator (??) - Sal Ferrarello
November 3, 2022 - The "or" (||) operator and the ... not exist. When dealing with strings you're typically better off using "or" (||) and for numbers you're typically better off using the "nullish coalescing operator" (??)....
🌐
GeeksforGeeks
geeksforgeeks.org › does-javascript-operators-like-or-and-null-coalescing-makes-weak-comparison
Does JavaScript Operators like or ( || ), and ( && ), null coalescing ( ?? ) makes Weak Comparison? | GeeksforGeeks
May 16, 2024 - Only if the value of x is nullish then the value of y will be assigned to x that means if the value of x is null or undefined then the value of y will be ass ... JavaScript Less Than or Equal(<=) to the operator is used to compare two operands ...
🌐
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 !,...
🌐
Zachsnoek
zachsnoek.com › blog › when-to-use-logical-or-vs-nullish-coalescing-in-javascript
When to use logical OR vs. nullish coalescing in JavaScript | Zach Snoek's Blog
We can make our previous example work as intended by using the nullish coalescing operator instead: const foo = 0; const bar = foo ?? 100; console.log(bar); // 0 · When creating default values, you should only use the logical OR operator when you want all falsy left operands to be considered invalid.
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
Nullish coalescing operator VS OR operator - JavaScript - The freeCodeCamp Forum
June 16, 2022 - please guys, is there any advantage of || over ?? . I see that ?? solves some of the problems that || can not solve
🌐
DEV Community
dev.to › hereisnaman › logical-or-vs-nullish-coalescing-operator-in-javascript-3851
JavaScript ||: Logical OR (||) vs Nullish Coalescing Operator (??) in JavaScript - DEV Community
April 22, 2020 - This operators returns the right hand value only if the left hand value is either null or undefined. const paginate = (options = {}) => { return [1, 2, 3, 4, 5].splice(0, options.limit ??
🌐
freeCodeCamp
freecodecamp.org › news › javascript-advanced-operators
Advanced JavaScript Operators – Nullish Coalescing, Optional Chaining, and Destructuring Assignment
January 4, 2024 - And that’s how you combine the nullish coalescing operator with either AND or OR operator. Like the nullish coalescing operator, the optional chaining operator is a modern addition to JavaScript that offers a better way to do things.
🌐
Fjolt
fjolt.com › article › javascript-nullish-coalescing
What is Nullish Coalescing (or ??) in Javascript
In Javascript, the nullish coalescing operator, or ?? operator is used to return the right hand side whenever the left hand side is null or undefined.