0

In JavaScript we can coerce any variable into a boolean using either !!myVar or Boolean(myVar).

But these coercions are based on whether the value is falsy.

I wanted to know if there's a way to make it based on whether the value is nullish.

I know how to do it with "long" code:

if (myVar ?? true) {...

where the condition will be coerced into false only if nullish, and true otherwise.

But I want to know if it's possible to do it with just one operator like with the falsy case (something like

if (??myVar) {...

or so). Like "syntactic sugar".

I think it would make my code much cleaner and readable.

Juan Perez
  • 212
  • 2
  • 10
  • 1
    I think that would make your code *less* readable, not the opposite. – InSync May 23 '23 at 11:29
  • 1
    `??` is already a `sugar`, before that we use `==null` or `=== undefined || === null` – spikef May 23 '23 at 11:39
  • @spikef yes. But since we have (!!var), why not have (??var) ? Seems logical. – Juan Perez May 23 '23 at 12:10
  • 2
    @JuanPerez `!!` is simply two invocations of the logical NOT (!) operator, so `!!!myVar` is just as valid (if redundant). The single `?` is the conditional operator and expects the remainder of a ternary statement after it and `??` is **already** the nullish coallescing operator. It's unclear what you actually expect the imagined operator to do, somehow make `null` and `undefined` truthy? – pilchard May 23 '23 at 12:25
  • 1
    Could you [edit] to remove the opinion line at the end of the question? It's not really relevant to the question as asked (you're asking "is this possible", not "would this be better", right?), and it's controversial enough that it invites debate that actively distracts from the question. – jcalz May 23 '23 at 13:25
  • 1
    I think your 'long' example is also getting in the way. `myVar ?? true` in a long-hand ternary is equivalent to `myVar === null || myVar === undefined ? true : myVar;` Which doesn't return a consistent type: `myVar = 1; // -> 1`, `myVar = true; // -> true`, `myVar = false; // -> false`, `myVar = null; // -> true` etc. You'll need to clarify the intended logic, but the short answer is 'no, there is no such operator'. – pilchard May 23 '23 at 13:35
  • 2
    Also, `myVar ?? true` doesn't always produce a boolean result so it's not doing what you say it's doing. You might could you change that to `myVar == null` which is the same number of characters but actually always produces a boolean depending on whether the `myVar` is nullish. Indeed, `myVar == null`, the "long" code, is the idiomatic (if weird) way of expressing this (EDIT: yeah, what pilchard said) – jcalz May 23 '23 at 13:36
  • @jcalz I wrote ```(myVar ?? true)``` in parenthesis, I was referring to using ```if (myVar ?? true)``` which will be coerced into ```false``` if nullish and ```true```otherwise. I'll clarify it in an exit. – Juan Perez May 23 '23 at 23:03
  • @pilchard i edited the question for more clarity. My intent is to use it inside an if condition, which already coerces its content into a bool using falsyness. But I want it to use nullishness instead. It can be just one `?`, or any other symbol, it was just an example. – Juan Perez May 23 '23 at 23:10
  • @JuanPerez No. `myVar ?? true` will not be coerced to `false` if nullish. It will be `true` if nullish, and if not nullish, will just be the value of `myVar`. – Bergi May 23 '23 at 23:18
  • As already noted multiple times in order to have your variable *'coerced into false only if nullish, and true otherwise'* you need simply negate the standard nullish check `!(myVar == null)` – pilchard May 24 '23 at 00:34
  • @pilchard …or use `myVar != null` – Bergi May 24 '23 at 01:17

1 Answers1

1

I'm affraid it's not possible. You have to keep using the nullish coalescing operator (??) or you could use your own function :

function isNullish(x) {
  return x === null || x === undefined;
}
Tom
  • 4,972
  • 3
  • 10
  • 28