Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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, 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 &&?

like image 509
f.khantsis Avatar asked Jul 16 '20 07:07

f.khantsis


People also ask

What is 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.

Can we combine optional chaining and Nullish coalescing?

Usage with optional chainingWe can combine nullish coalescing with the optional chaining to achieve that.

Is Nullish undefined?

A nullish value is a value that is either null or undefined .

Is empty string Nullish?

Instead, with the Nullish Coalescing operator, an empty string is not null, and therefore the expression evaluates to the left-hand value.

What is the nullish coalescing operator?

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.

What is the difference between the null-coalescing operator?

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 ??

What is optional chaining and nullish coalescing?

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.

How do I evaluate an expression with a null-conditional operator?

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:


3 Answers

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)
like image 126
str Avatar answered Oct 23 '22 21:10

str


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.

like image 34
Luis Pais Avatar answered Oct 23 '22 20:10

Luis Pais


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
like image 1
Teamur Avatar answered Oct 23 '22 21:10

Teamur