nullish operator like ! operator

I liked the ?? operator they added recently. But it would also be nice if there was an is null like operator that worked similar to NOT (!).

!undefined // true
!null // true
!false // true
!'' // true
!0 // true

// something like

?undefined // true
?null // true
?false // false
?'' // false
?0 // false

You can just write value == null for that.

2 Likes

So you can, I didn't know that. Thanks!

I'm personally a bit uncomfortable using loose equality in Javascript.

Actually I think this idea would be a good feature to add to the language. When you look at the latest operators added to the language, we can see similar operators like ?. , ?? , or ??= , achieving the same purpose: easing comparisons with nullish values. This proposal would stay consistent with them.

1 Like

However, I would treat the ? operator in the same spirit than the ?? operator, instead of the ! one.
Like:

if (?myVar) {
  // do something if myVar is defined (i.e. not undefined or null)
}

and the negation:

if (!?myVar) {
  // do something if myVar is nullish
}
1 Like