Opposite of nullish coalescing operator

12.2k views Asked by At

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

6

There are 6 answers

6
str On BEST ANSWER

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)
1
Kolby Watson On

I don't like it but here's my solution:

output = typeof input === 'boolean' && "other"

Truth table:

input     -> output
--------------------
null      -> false
undefined -> false
true      -> "other"
false     -> "other"
0
Luis Pais On

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.

0
Sebastian On

There's currently no such operator, as others have already pointed out. However, there are a few proposals for exactly this operator in the official TC39 discourse group:

If you're interested in bringing this into ECMA, these might be good places to start.

0
Bhojendra Rauniyar On

I just found this post today, and I thought to add my few words on it.

Although, inverse null coalescing operator is in the proposal:

a = b !? func(b)

I don't think this proposal will be implemented and is a must have feature since we can simply get desired behaviors with && and || operators. The add of ?? is special feature and is a must have feature. This is just my opinion.

I don't think we should wait than simply using like:

a = func(b??false)

We just need to be creative! I have used here nullish coalescing operator directly inside the function call so we avoided the need of inversing it like:

a = b != null && func(b) or so on

So, I opine to have !? is completely unnecessary. We just need to think it correctly.

"Something not null then have it" can be simply be denoted with nullish coalescing operator: (HAVE_IT ?? false) || (HAVE_IT ?? 0) || (HAVE_IT ?? ANYTHING_YOU_WANT_EXCEPT_NULL).

0
Teamur On

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