Questions tagged [nullish-coalescing]
34 questions
51
votes
6 answers
Opposite of nullish coalescing operator
Nullish coalescing operator allows assigning a variable if it's not null or undefined, or an expression otherwise.
a = b ?? other
It is an improvement over previously used || because || will also assign other if b is empty string or other falsy,…

f.khantsis
- 3,256
- 5
- 50
- 67
21
votes
2 answers
How is the nullish coalescing operator (??) different from the logical OR operator (||) in ECMAScript?
ES2020 introduced the nullish coalescing operator (??) which returns the right operand if the left operand is null or undefined. This functionality is similar to the logical OR operator (||). For example, the below expressions return the same…

francis
- 3,852
- 1
- 28
- 30
10
votes
1 answer
Why does the nullish coalescing operator not work as a typeguard in typescript?
With Typescript 3.7 the nullish coalescing operator was introduced. It would seem to be the perfect type guard for cases like
const fs = (s: string) => s
const fn = (n: number) => n
let a: string | null | undefined
let b: number | null |…

gwildu
- 3,621
- 1
- 17
- 22
8
votes
3 answers
Safe destructuring using nullish coalescing or optional chaining
Currently I am using below code for destructuring:
const myObj1 = {name: 'Abc'}
const {name} = myObj1
console.log(name)
const myObj2 = null
const {name2} = myObj2 // this will give error
Now, since we have optional chaining, I can do…

Adarsh Madrecha
- 6,364
- 11
- 69
- 117
7
votes
5 answers
Difference between && and ?? in JavaScript
I am trying to use Logical AND && and Nullish coalescing operator ?? operators for the conditional rendering of variables/values.
But for some reason, I am unclear about the usage of both of these operators and how they work.
Please explain the…

Ahmad Habib
- 2,053
- 1
- 13
- 28
7
votes
6 answers
Javascript - Replace undefined with 0
I have a variable that gets defined by user input. I want to replace its value only if it's undefined. But not if it's NaN. How can I do it?
I tried doing x || 0 but that also replaces NaN values.

user3312508
- 907
- 4
- 10
- 25
4
votes
2 answers
Why javaScript's Optional Chaining Syntax does not work with Nullish Coalescing Operator when there is no method's return value?
I"m trying the new JavaScript Optional Chaining syntax and it seems that it has an issue with methods which has no return value.
For example:
I have a restaurant object with order method in it:
const restaurant = {
order(i, k) {
console.log(`I…

Irfan
- 4,882
- 12
- 52
- 62
3
votes
3 answers
Is there a reverse logical nullish assignment?
So the ??= operator assigns the value to the variable only if the current stored value is nullish.
Maybe I'm missing the obvious but I can't think of a slick solution (without if statements) to only assign if the value is not nullish?
I'm using…

Proxycon
- 71
- 7
3
votes
3 answers
Any difference between x || [] and x ?? [] in javascript?
I've see both x || [] and x ?? [] used for providing a fallback value if x is nullish. Are there any cases where these two give different results?

JOrielly
- 31
- 2
3
votes
2 answers
Can you destruct an object and have nullish coalescing for potential undefined constants?
I use React, so I have a props object, for example:
const props: { id: number, name?: string} = { id: 1 }; // not defining the `name`
const { id, name } = props; // here the `const name` becomes undefined forever and even if I use the defaultProps…

Nikolai
- 555
- 7
- 17
2
votes
2 answers
Will the Logical nullish Assignment or nullish coalescing evaluate the assigned value in a non null case?
I want to understand if my use case will benefit from the logical nullish assignment operator.
I'm checking my database to see it some data exists, otherwise I fetch it from an API, however I don't want to fetch the data from the API if the data…

rishi
- 652
- 1
- 6
- 20
1
vote
3 answers
Nullish Coalescing is not working in this case [solved]
In the following code I want to print out the "dayNumber" property, in case the item is an object otherwise It should print out the number 0.
I can't get rid of the error and I don't understand why.
I'm trying to use the nullish coalescing…

Cristian Cassetta
- 33
- 4
1
vote
0 answers
JavaScript 'Nullish coalescing operator' throwing an Error if it is null?
I was hoping for a very concise
video = document.querySelector('#video') ?? throw new Error("Unable to find #video");
But that gives me a "Uncaught SyntaxError: Unexpected token 'throw'"
Is there an easy way to natively do this with the ?? or ?.…

Benjamin H
- 5,164
- 6
- 34
- 42
1
vote
1 answer
Is there a way to utilize the nullish coalescing operator (`??`) in object property destructuring?
In ReactJS, I commonly use this pattern of destructurnig props (I suppose it is quite idiomatic):
export default function Example({ ExampleProps }) {
const {
content,
title,
date,
featuredImage,
author,
tags,
} =…

HynekS
- 2,738
- 1
- 19
- 34
1
vote
0 answers
Logical and (&&) type of operator only for null types in Javascript
The logical && operator returns the left side iff it is evaluated as 'falsy'.
null && 10
> null
0 && 10
> 0
1 && 10
> 10
The nullish coalescing operator (??) returns the left side iff it is not null or undefined.
null ?? 10
> 10
0 ?? 10
> 0
1…

phaze
- 152
- 7