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
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'
๐ŸŒ
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).
๐ŸŒ
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.

๐ŸŒ
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 ??
๐ŸŒ
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 - If you want to check and avoid printing empty values, use the Logical OR Operator (||). console.log("" || "Hello") // "Hello" console.log("" ?? "Hello") // "" If 0 or Boolean false is a valid return value in your use-case, use ??
๐ŸŒ
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...
๐ŸŒ
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
Find elsewhere
๐ŸŒ
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 !,...
๐ŸŒ
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 - This means that if the left value of the OR is a non-Boolean value, it will be converted to a Boolean value before the operator is applied. ... let value = ""; let orSolution = value || "default value"; let nullishSolution = value ?? "default value" console.log(orSolution) // defaut value console.log(nullishSolution) // "" 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.
๐ŸŒ
Atomizedobjects
atomizedobjects.com โ€บ blog โ€บ javascript โ€บ nullish-coalescing-vs-or-javascript-operators
Nullish Coalescing vs OR - JavaScript operators (?? vs ||) | Atomized Objects
If you are still not 100% sure what this means at this point, that is okay, it will become more obvious when we compare the Nullish Coalescing Operator to the logical OR operator. The Logical OR Operator is written as two pipe symbols in JavaScript (||) and it looks at the left hand value (a || b) to see it is a falsy value.
๐ŸŒ
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 ??
๐ŸŒ
Sonar Community
community.sonarsource.com โ€บ sonarqube for ide
JS/TS rule S6606 suggestion to replace || with ? is ambiguous - SonarQube for IDE - Sonar Community
June 22, 2023 - Rule S6606 (JS/TS) suggests to โ€œPrefer using nullish coalescing operator (??) instead of a logical or (||), as it is a safer operatorโ€. The operators have different behavior. false ??
๐ŸŒ
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
When creating default values, you should only use the logical OR operator when you want all falsy left operands to be considered invalid. For example, if you want to set a default when the left operand is the empty string, use the logical OR.
๐ŸŒ
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.
๐ŸŒ
Softwareshorts
softwareshorts.com โ€บ nullish-coalescing-vs-or-javascript-operators
Nullish Coalescing vs OR - JavaScript operators (?? vs ||) | SoftwareShorts
Unlike the logical OR operator (||), which considers falsy values such as an empty string or zero as "falsey" and falls back to the default value, the Nullish Coalescing Operator only checks for null or undefined values. This operator is particularly useful when you want to assign a default ...
๐ŸŒ
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).
๐ŸŒ
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.
๐ŸŒ
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;
}
๐ŸŒ
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 ...