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, but not nullish value.
However, sometimes, we also use &&
for value assignment, for example
a = b && func(b)
where we only want to do func
on b
if it's not nullish, otherwise assign the nullish b
.
Of course, &&
checks for falsiness, not nullishness. Is there a nullish version of &&
?
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.
Usage with optional chainingWe can combine nullish coalescing with the optional chaining to achieve that.
A nullish value is a value that is either null or undefined .
Instead, with the Nullish Coalescing operator, an empty string is not null, and therefore the expression evaluates to the left-hand value.
Nullish coalescing operator (??) 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.
The null-coalescing operator ?? returns the value of its left-hand operand if it isn't null; otherwise, it evaluates the right-hand operand and returns its result. The ?? operator doesn't evaluate its right-hand operand if the left-hand operand evaluates to non-null. Available in C# 8.0 and later, the null-coalescing assignment operator ??
Optional chaining and nullish coalescing both are available to use in JavaScript with the launch of ES2020 aka ES11. The optional chaining operator, ?., permits reading the value of a property located deep within a chain of connected objects without having to expressly validate that each reference in the chain is valid.
In expressions with the null-conditional operators ?. and ? [], you can use the ?? operator to provide an alternative expression to evaluate in case the result of the expression with null-conditional operations is null:
To my knowledge, there is no such operator and also no proposal to add one. Instead you can rely on the standard way to check for nullish values: b == null
a = b == null ? b : func(b)
This will not answer the question since it was already answered by @str, I'm just posting this here because I don't have enough rep to comment on @Dalou's answer and don't want people to trip on that answer.
a = (b ?? false) && other
Is not the opposite of ??
, since a
will take the value of b
if b
is a falsy value other than undefined/null, like ''
or 0
for example.
The opposite of ??
should set a
to the value of other
even if b
is ''
or 0
.
ANSWER:
let a = 9;
if( a!==null || a!==undefined ){ /* actions */ };
if( (a??null) !== null ){ /* actions */ }
PROPOSAL:
const oo = console.log;
let a; // undefined, null
// Syntax proposal: !?
a !? oo('NOT Nullish coalescing operator');
// Logical NOT Nullish assignment:
let a = 7;
a !?= 5;
oo(a); // 5
let b, c = null;
b !?= 3;
oo(b); // undefined
c !?= 8;
oo(c); // null
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With