It's a bit unclear what you're asking. This code:

const location = {
  lat: data?.coords?.latitude,
  long: data?.coords?.longitude
}

assigns undefined to lat if either data or data.coords is null or undefined (rather than throwing an error). But if data and data.coords are both not null or undefined, it assigns data.coords.latitude and data.coords.longitude to lat and long. It's the new optional chaining operator.

The next line also uses it, but:

  1. If the uid could be a falsy value (like "" or 0), you should use the new nullish coalescing operator as well: ?? rather than ||.

  2. If auth may be null or undefined, you need another ?.

So:

const docId = auth?.currentUser?.uid ?? '123'
Answer from T.J. Crowder 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.
🌐
JavaScript.info
javascript.info › tutorial › the javascript language › javascript fundamentals
Nullish coalescing operator '??'
April 23, 2023 - The precedence of the ?? operator is the same as ||. They both equal 3 in the MDN table. That means that, just like ||, the nullish coalescing operator ??
Discussions

What is the ?. operator and Where I can the use ?? Nullish ...
I found this pieces of code: Here, What is the ?. JavaScript operator? And Will I be able to use ?? Nullish Coalescing Operator in JavaScript instead of || OR operator according to the mentioned code More on stackoverflow.com
🌐 stackoverflow.com
TIL about the Nullish Coalescing Operator in Javascript (??)
Wait until you learn about the elvis operator More on reddit.com
🌐 r/ProgrammerTIL
18
78
March 30, 2022
When should I use ?? (nullish coalescing) vs || (logical OR)?
And more recently, the ?? operator was included in ES2020, which is supported by Node 14 (released in April 2020). When the nullish coalescing operator ?? More on stackoverflow.com
🌐 stackoverflow.com
Why doesn't JavaScript have a nullish-coalescing-assignment (??=) operator?
Part of ES2021: https://github.com/tc39/proposal-logical-assignment More on reddit.com
🌐 r/javascript
7
2
July 31, 2021
🌐
W3Schools
w3schools.com › jsref › jsref_oper_nullish.asp
W3Schools.com
The ?? operator returns the right operand when the left operand is nullish (null or undefined), otherwise it returns the left operand.

It's a bit unclear what you're asking. This code:

const location = {
  lat: data?.coords?.latitude,
  long: data?.coords?.longitude
}

assigns undefined to lat if either data or data.coords is null or undefined (rather than throwing an error). But if data and data.coords are both not null or undefined, it assigns data.coords.latitude and data.coords.longitude to lat and long. It's the new optional chaining operator.

The next line also uses it, but:

  1. If the uid could be a falsy value (like "" or 0), you should use the new nullish coalescing operator as well: ?? rather than ||.

  2. If auth may be null or undefined, you need another ?.

So:

const docId = auth?.currentUser?.uid ?? '123'
Answer from T.J. Crowder on Stack Overflow
🌐
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.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-nullish-coalescing-operator
JavaScript Nullish Coalescing(??) Operator - GeeksforGeeks
The nullish coalescing (??) operator is used to handle null and undefined values in JavaScript.
Published   January 22, 2026
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 - When used in an expression, the Nullish Operator checks the first operand (on the left) to see if its value is null or undefined. If it isn't, the operator returns it from the expression.
🌐
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).
🌐
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.
🌐
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;
}
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'
🌐
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 these two operators is how they handle falsy 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.
🌐
Go Make Things
gomakethings.com › the-nullish-coalescing-operator-in-vanilla-js-sorry-the-what-now
The nullish coalescing operator in vanilla JS (sorry, the what now?) | Go Make Things
June 30, 2020 - It works a lot like the or operator, but instead of checking for all falsy values, it only runs if the value on the left side is null or undefined. // nullish coalescing operator var logCount = function (num) { console.log(`There are ${num ??
🌐
TypeScript
typescriptlang.org › play › 3-7 › syntax-and-messaging › nullish-coalescing.ts.html
TypeScript: Playground Example - Nullish Coalescing
The nullish coalescing operator is an alternative to || which returns the right-side expression if the left-side is null or undefined. In contrast, || uses falsy checks, meaning an empty string or the number 0 would be considered false.
🌐
Alishoff
alishoff.com › blog › 230
Как использовать Nullish Coalescing (оператор нулевого ...
Вместо этого он использует nullish, что говорит о том, что значение должно быть либо null, либо undefined.
🌐
daily.dev
daily.dev › home › blog › webdev › nullish coalescing operator (??) in javascript - what is it and how to use it?
Nullish Coalescing Operator (??) In JavaScript - What Is It And How To Use It?
November 1, 2021 - The Nullish Coalescing Operator allows us to check if a value is null or undefined, and provide a fallback value if that is the case.
🌐
freeCodeCamp
freecodecamp.org › news › javascript-advanced-operators
Advanced JavaScript Operators – Nullish Coalescing, Optional Chaining, and Destructuring Assignment
January 4, 2024 - The nullish coalescing operator is useful in a variety of situations where you need to check for null or undefined values and provide a default value.
🌐
Can I Use
caniuse.com › mdn-javascript_operators_nullish_coalescing
JavaScript operator: Nullish coalescing operator (`??`) | Can I use... Support tables for HTML5, CSS3, etc
"Can I use" provides up-to-date browser support tables for support of front-end web technologies on desktop and mobile web browsers.
🌐
TutorialsPoint
tutorialspoint.com › home › javascript › javascript nullish coalescing operator
JavaScript Nullish Coalescing Operator
September 1, 2008 - It is a logical operator introduced in ES2020. In many cases, we can have the null or empty values stored in the variables, which can change the behavior of the code or generate errors.
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › csharp › language-reference › operators › null-coalescing-operator
?? and ??= operators - null-coalescing operators - C# reference | Microsoft Learn
January 24, 2026 - The `??` and `??=` operators are the C# null-coalescing operators. They return the value of the left-hand operand if it isn't null. Otherwise, they return the value of the right-hand operand