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 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.
🌐
Eran Stiller
eranstiller.com › javascript-double-question-marks
What Is the JavaScript Double Question Marks (??) Operator?
September 24, 2023 - The double question marks operator offers a reliable, concise way to set default values and avoid errors in your code. Remember, it’s all about preventing those pesky null or undefined values from causing havoc in your JavaScript programs.
Discussions

javascript - When should I use ?? (nullish coalescing) vs || (logical OR)? - Stack Overflow
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 = More on stackoverflow.com
🌐 stackoverflow.com
What does the !! (double exclamation mark) operator do in JavaScript? - Stack Overflow
I saw this code: this.vertical = vertical !== undefined ? !!vertical : this.vertical; It seems to be using !! as an operator, which I don't recognize. What does it do? More on stackoverflow.com
🌐 stackoverflow.com
javascript - What is double question mark equal? - Stack Overflow
Communities for your favorite technologies. Explore all Collectives · Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work More on stackoverflow.com
🌐 stackoverflow.com
New Nullish Coalescing (?? Double question mark ) Operator in Typescript & Javascript

It's very recent, it was moved to stage 4 (Finished) 5 months ago https://github.com/tc39/proposal-nullish-coalescing

More on reddit.com
🌐 r/javascript
6
4
May 13, 2019
🌐
W3Schools
w3schools.com › jsref › jsref_oper_nullish.asp
JavaScript Nullish Coalescing Operator
The ?? operator returns the right operand when the left operand is nullish (null or undefined), otherwise it returns the left operand. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: ...
🌐
CoreUI
coreui.io › blog › what-is-double-question-mark-in-javascript
What is Double Question Mark in JavaScript? · CoreUI
March 12, 2024 - Embrace the power of ?? in your projects and watch your coding practices evolve to new heights. The double question mark, or the Nullish Coalescing Operator, checks if the value to its left is null or undefined.
🌐
Flexiple
flexiple.com › javascript › double-question-mark-javascript
The Double Question Mark (??) in JavaScript - Flexiple
The ?? operator in JavaScript is known as the nullish coalescing operator. It returns the right-hand operand when the left-hand operand is either null or undefined. This operator provides a reliable way to specify default values.
🌐
Scaler
scaler.com › home › topics › what is a nullish coalescing operator or double question mark (??) in javascript?
What is JavaScript double question mark (??) - nullish coalescing operator - Scaler Topics
March 28, 2024 - The Nullish coalescing operator ... question mark is a logical operator that takes two values and returns the right-hand value if the left-hand value is undefined or null, Else returns the left-hand operand....
Find elsewhere
🌐
EyeUniversal
eyeuniversal.com › home › tech › deep dive into javascript's double question mark operator
Mastering JavaScript's Double Question Mark Operator
July 23, 2024 - Dive into JavaScript's Double Question Mark Operator (??). Discover how it handles null/undefined values and enhances your coding skills.
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_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).
🌐
freeCodeCamp
freecodecamp.org › news › javascript-advanced-operators
Advanced JavaScript Operators – Nullish Coalescing, Optional Chaining, and Destructuring Assignment
January 4, 2024 - The double question mark is a logical operator that returns the expression on the right-hand side of the mark when the expression on the left-hand side is null or undefined · This operator is also known as the nullish coalescing operator.
🌐
Medium
medium.com › @danialashrafk20sw037 › javascript-double-question-mark-57ebb6c6be55
JavaScript Double Question Mark. Introduction to JavaScript Double… | by Danialashrafksw | Medium
March 8, 2024 - The double question mark (`??`) operator, also known as the nullish coalescing operator, is designed to provide a concise way to handle default values for variables that may be `null` or `undefined`. It differs from the logical OR (`||`) operator ...
🌐
BetterBugs
betterbugs.io › blog › javascript-double-question-mark-nullish-coalescing-operator
What is JavaScript Double Question Mark (??) or the Nullish Coalescing Operator?
April 10, 2024 - Simply put, the double question mark (??) operator returns the right-hand operand if the left-hand operand evaluates to a nullish value (either null or undefined).
🌐
SkillReactor Blog
skillreactor.io › home › double question mark javascript operator explained
Double Question Mark JavaScript Operator Explained - SkillReactor Blog
July 8, 2024 - This operator, known as the nullish coalescing operator, is a powerful tool for writing more robust JavaScript applications. When handling null or undefined values, the double question mark operator offers a concise and efficient solution to streamline your code and improve its readability.
🌐
Codedamn
codedamn.com › news › javascript
Double question mark in JavaScript (Nullish coalescing operator)
January 5, 2023 - In conclusion, the nullish coalescing operator is a new JavaScript feature introduced in the ECMAScript 2020 specification. It is represented by two consecutive question marks (??) and is used to provide a default value for null or undefined values.
🌐
Altcademy
altcademy.com › blog › what-is-double-question-mark-in-javascript
What is double question mark in JavaScript
September 17, 2023 - The double question mark (??) is you checking for eggs and deciding what to use. let eggs = null; let substitute = 'applesauce'; let ingredient = eggs ?? substitute; console.log(ingredient); // "applesauce" In the code above, because eggs is ...
🌐
vteams
vteams.com › blog › what-is-question-mark-in-javascript
Javascript's Double Question Mark: Meaning and Usage
April 23, 2024 - Then using the || operator this function will call the default value. Question mark Javascript can be used to fix this error. javascript double question mark “??” are used instead of || and it is also called the Nullish Coalescing.