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
🌐
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...
🌐
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 ??
🌐
Medium
developerchandan.medium.com › vs-in-javascript-the-little-known-difference-500ddb55a9bd
?? vs || in JavaScript: The little-known difference | by Chandan Kumar | Medium
June 25, 2024 - In JavaScript, both the ?? (nullish coalescing operator) and || (logical OR operator) are used to provide default values, but they have a crucial difference in terms of how they handle falsy values.
🌐
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
🌐
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 - However they have few differences which we will explore bellow: The OR operator uses the right value if left is falsy. This means that if the left value of the logical OR operator is truthy but not nullish, the right value will not be evaluated.
Find elsewhere
🌐
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.
🌐
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 solves the problem for us. This operators returns the right hand value only if the left hand value is either null or undefined.
🌐
DEV Community
dev.to › vincent-delmotte › le-saviez-vous-vs--3c03
Nullish Coalescing Operator vs Logical Or Operator - DEV Community
August 27, 2024 - De son doux nom français “Opérateur de coalescence des nuls”, a ?? b permet de renvoyer le terme a si ce dernier n’est pas ni null ni undefined. Dans le cas inverse l’opérateur renvoie le terme b. Voici un exemple qui permet de redéfinir cet opérateur en JavaScript. ... const nullishCoalescingOperator = (a, b) => { if (a !== null && a !== undefined) { return a } return b; } const result = nullishCoalescingOperator(a,b);
🌐
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 - It only considers null and undefined as nullish values. Logical OR (||): This is useful when you want a default value if the left operand is any falsy value.
🌐
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.
🌐
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.
🌐
Designcise
designcise.com › web › tutorial › what-is-the-difference-between-javascript-nullish-coalescing-and-or-operator
JavaScript ?? vs. || – What's the Difference? - Designcise
December 1, 2021 - However, they differ in the following ways: The OR operator returns the value on the right if the expression on the left is falsy, while the nullish coalescing operator returns the value on the right if expression on the left is a nullish value ...
🌐
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 - So, the nullish operator is related to null and undefined values while other logical operators are related to truthy and falsy values in general. Coalescing, according to the dictionary means "coming together to form one whole".
🌐
Glasp
glasp.co › hatch › 0xlhky7w000c0p8d › p › B46O8tKrnZeu2ZHcStj7
Understanding Nullish Coalescing and Logical OR in JavaScript: A Guide for Developers | Glasp
4 weeks ago - This means that if the left operand evaluates to any of these values, the result will be the right operand. ... On the other hand, the nullish coalescing operator (??) is more specific.
🌐
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 - However, you must handle cases where height or weight may be missing or null. Here’s an example that initially doesn’t work as intended: let height = 175; // in centimeters let weight = null; // weight is missing let bmi = height ?? 100 * weight ?? 50; // This doesn't work as intended · In this code, JavaScript evaluates 100 * weight before applying the nullish coalescing operator, which isn't what we want.
🌐
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 ??