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'
🌐
MSR
rajamsr.com › home › double question mark in javascript (??): a comprehensive guide
Double Question Mark In JavaScript (??): A Comprehensive Guide | MSR - Web Dev Simplified
March 3, 2024 - Pro Hint: JavaScript Double pipe vs double question mark In simple, the double question mark operator just checks for null and undefined values, but the double pipe operator checks for all of the falsy values specified in the preceding table.
🌐
Mayallo
mayallo.com › home › shorts › the double question mark (nullish coalescing operator) in javascript
The Double Question Mark (Nullish Coalescing Operator) in JavaScript | Mayallo
April 16, 2025 - In this article, we knew that the Double Question Mark (Nullish Coalescing Operator) returns the right expression if the left expression was ONLY null or undefined.
🌐
EyeHunts
tutorial.eyehunts.com › home › javascript double question mark vs double pipe | code
JavaScript double question mark vs double pipe | Code
July 27, 2023 - The OR operator (double pipe) || uses the right value if the left is falsy, while the nullish coalescing operator ?? (double question mark) uses the right value if the left is null or undefined.
🌐
InfoWorld
infoworld.com › home › software development › programming languages › javascript
8 new JavaScript features you might have missed | InfoWorld
February 10, 2022 - Nullish coalescing is also a new ... OR operator (the double pipe symbol ||). The difference between ?? and || is in how the operator handles nullish versus falsy values....
🌐
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.
🌐
egghead.io
egghead.io › lessons › javascript-double-and-double-or-double-functionality
Double && (and) Double || (or) = Double Functionality | egghead.io
June 30, 2015 - [00:00] The double ampersand and double pipe are logical operators in JavaScript, but we can do some pretty cool shorthand conditional operations with them. "[00:11] What's more fun -- Programming or Testing? Testing or Programming?" If I asked you if those two questions would have the same result, one would typically say yes, but in JavaScript, they will have two different answers because the order in which you ask the question has an impact.
🌐
Tim Mousk
timmousk.com › blog › typescript-double-question-mark
How To Use The Double Question Mark Operator In TypeScript? – Tim Mouskhelichvili
March 6, 2023 - As you can see, the double pipe operator falls back to the default value when the variable is falsy. As you can see, the nullish coalescing or double question mark operator becomes handy when a developer needs to provide a default value for ...
Find elsewhere
🌐
CoreUI
coreui.io › blog › what-is-double-question-mark-in-javascript
What is Double Question Mark in JavaScript? · CoreUI
March 12, 2024 - As of its introduction in ECMAScript 2020, the ?? operator is supported in most modern browsers and JavaScript environments, including Node.js versions that are based on newer V8 engines. However, for older environments or browsers that do not support ECMAScript 2020 features, a transpiler like Babel may be required to ensure compatibility. The double question mark operator enriches JavaScript’s syntax by offering a clear and concise way to handle null and undefined values.
🌐
Reddit
reddit.com › r/javascript › new nullish coalescing (?? double question mark ) operator in typescript & javascript
r/javascript on Reddit: New Nullish Coalescing (?? Double question mark ) Operator in Typescript & Javascript
May 13, 2019 - Chat about javascript and javascript related projects. Yes, typescript counts. Please keep self promotion to a minimum/reasonable level. ... It's very recent, it was moved to stage 4 (Finished) 5 months ago https://github.com/tc39/proposal-nullish-coalescing ... Wow, I just typed in this search on duckduckgo "double question mark typescript" after reading some source code from GitHub...
🌐
Google APIs
storage.googleapis.com › drgtuyeqgkpuze › double-question-mark-vs-double-pipe-javascript.html
Double Question Mark Vs Double Pipe Javascript at Amanda Barbour blog
December 13, 2024 - The nullish coalescing operator, ... that takes. To know the difference between ||. The or operator (double pipe) || uses the right value if the left is falsy, while the nullish....
🌐
sebhastian
sebhastian.com › javascript-double-question-mark
Learning JavaScript double question mark (??) or the Nullish Coalescing Operator | sebhastian
January 23, 2022 - The double question mark is a logical operator that returns the expression on the right-hand of the mark when the expression on the left-hand is null or undefined · This operator is also known as the Nullish Coalescing Operator.
🌐
Eran Stiller
eranstiller.com › javascript-double-question-marks
What Is the JavaScript Double Question Marks (??) Operator?
September 24, 2023 - With the double question marks operator, you can streamline this process. ... If yourVariable is null or undefined, result will automatically be set to defaultValue. It’s a more efficient, readable way to handle default value assignments in JavaScript.
🌐
Michael Currin
michaelcurrin.github.io › dev-cheatsheets › cheatsheets › javascript › general › control-flow › double-question-mark.html
Double question mark | Dev Cheatsheets
Cheatsheets / JavaScript / General ... x !== undefined) ? x : y; Note that this does not check if x is truthy. If use double pipe syntax instead, then if x is an empty string, zero, null, undefined, etc....
🌐
YouTube
youtube.com › watch
One Of The Most Important JavaScript Features - Nullish Coalescing (??) in 2 Minutes - YouTube
The Nullish Coalescing Operator (??) in JavaScript lets you set default values for when things are missing. It's similar to Logical OR (||) but BETTER.For yo...
Published   September 3, 2024
🌐
Mikatour
mikatour.com.tw › post › js-double-question-mark-vs-double-pipe
js double question mark vs double pipe
September 28, 2024 - The logical OR operator (||) returns the right-hand operand when the left-hand operand evaluates to a falsy value. In JavaScript, the falsy values are false, 0, "" (empty string), null, undefined, and NaN.
🌐
xjavascript
xjavascript.com › blog › typescript-double-question-mark-vs-double-pipe
TypeScript: Double Question Mark vs Double Pipe — xjavascript.com
This blog post aims to provide a comprehensive comparison between these two operators, covering their fundamental concepts, usage methods, common practices, and best practices. ... The double pipe operator (||) is a logical OR operator. In ...