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:
If the
uidcould be a falsy value (like""or0), you should use the new nullish coalescing operator as well:??rather than||.If
authmay benullorundefined, you need another?.
So:
const docId = auth?.currentUser?.uid ?? '123'
Answer from T.J. Crowder on Stack OverflowWhat is the ?. operator and Where I can the use ?? Nullish ...
TIL about the Nullish Coalescing Operator in Javascript (??)
When should I use ?? (nullish coalescing) vs || (logical OR)?
Why doesn't JavaScript have a nullish-coalescing-assignment (??=) operator?
Videos
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:
If the
uidcould be a falsy value (like""or0), you should use the new nullish coalescing operator as well:??rather than||.If
authmay benullorundefined, you need another?.
So:
const docId = auth?.currentUser?.uid ?? '123'
Answer from T.J. Crowder on Stack OverflowOften 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;
}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).
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'