60

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

7
  • 4
    To my knowledge, there is no such operator (and not even a proposal). But you can use the following instead: a = b == null ? b : func(b).
    – str
    Commented Jul 16, 2020 at 7:22
  • 1
    double equal sign with null sounds scary
    – f.khantsis
    Commented Jul 16, 2020 at 7:22
  • 5
    Why? It is the way to check for nullish values.
    – str
    Commented Jul 16, 2020 at 7:25
  • 1
    @f.khantsis x == null will only return true if x = null or x = undefined. As such, it's the exact match of the nullish coalescing behaviour which only activates for those values.
    – VLAZ
    Commented Jul 16, 2020 at 7:25
  • 4
    @StackSlave "do func on b if it's not nullish, otherwise assign the nullish b" seems exactly what my code is doing.
    – str
    Commented Jul 16, 2020 at 7:38

6 Answers 6

23

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)
6
  • If b is false, with this approach is treated as an undefined or null, so trigger func(b)
    – Chu
    Commented May 19, 2021 at 3:31
  • @Chu If b is nullish (only null or undefined), the function will not run. If b is anything else (including false) the function will run, and its result will be assigned to a. Try running false == null in your browser console. Commented Aug 3, 2021 at 2:31
  • How about a = b == null && func(b)? Or if your linting config forbids the use of ==, then a = (b === null || b === undefined) && func(b) Commented Feb 5, 2022 at 0:46
  • 1
    Oh my, I've used so many triple equals === that I completely forgot about undefined == null.
    – Qwerty
    Commented Aug 26, 2022 at 2:18
  • 1
    @MattBrowne Even if your linter forbids ===, it should still allow == null as a special case. However, you end up with false | T in this case, which is not great (especially if you plan to use it again in the same way).
    – riv
    Commented Jun 12, 2023 at 7:49
8

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.

5

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
5

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

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"
1
  • Note that this will return false if input is some other value, like a string Commented Feb 5, 2022 at 0:39
0

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).

Not the answer you're looking for? Browse other questions tagged or ask your own question.