What?
In short, the idea is to have a ?
operator to test whether a variable is nullish or not.
if (?a) {
// do something if a is defined (i.e. not undefined or null)
}
Instead of having to write:
if (a !== undefined && a !== null) {
// do something
}
Why?
The main reason is to continue to simplify and shorten nullish variable comparison.
Over the past years, more and more ways of handling nullish values were introduced in the language:
- the optional chaining operator
?.
- the nullish coalescing operator
??
- the logical nullish assignment
??=
Those additions were very handy, but something is still missing: what if we simply want to test whether a variable is nullish or not?
We would still have to write something like:
if (a !== undefined && a !== null)
// OR using loose comparison
if (a != null)
// But using loose comparison is not a good practise, as it can be more confusing (and slower)
// (and moreover it does not handle the special `document.all` case!)
On the other hand, if we just want to see if a variable is falsy, we'd just have to write:
if (!a)
which is very short to type and to read.
The problem here is that the !
matches any falsy values, like ""
, NaN
or even 0
.
That would be great to have such a syntax, but to only check whether the variable is nullish or not.
How?
So:
if (?a) {
// do something if a is defined
}
would simply be syntactic sugar for:
isDefined(variable) {
return variable !== undefined && variable !== null;
}
if (isDefined(a)) {
// do something if a is defined
// note that the the variable is only evaluated once
}
Operator precedence
The operator precedence might be at the same level than the logical not operator (level 17), with left-to-right associativity.
Handling undeclared variables
I don't think that the ?
operator should handle undeclared variables, like when using the typeof syntax: typeof a === "undefined"
.
The other nullish operators ??
and ?.
throw a ReferenceError
if the variable is not declared, so it would be coherent that the ?
does the same.
Pros & Cons
I'd like to discuss several pros and cons about this proposal:
Pros
Readability
I personally find this syntax short, clean and understandable.
What's more, we remove the double negation of the evaluation:
a !== undefined
is read as: "a
not undefined", whereas:
?a
is read directly as: "a
defined"
We can also easily invert the condition, testing if the variable is not defined, like:
if (!?a)
In this case, the syntax still remains readable and easy to understand.
Backward compatibility
The proposed ?
operator should not break any previous code.
While the ?
operator is already used for ternary condition condition ? expressionTrue : expressionFalse
, as it expects 3 members and a second :
operator, there is not risk that it would be mistaken by Javascript engines / parsers.
Usage with optional chaining
This operator also goes really well alongside the optional chaining operator:
const obj = { sub: { value: 0 } };
if (?obj?.sub?.value) {
// do something if obj.sub.value is defined
}
Cons
Possibly confusing syntax
The proposed syntax could be confusing, especially when using alongside ternary conditions.
For instance, the expression: ?a ? ?a : b ?? ?c
is a real pain to read, while being totally valid.
But who is going to write such awful code like this in prod anyway? (:
Yet another way to check for undefined
There are already (too) many different ways to evaluate undefined variables, each one coming with their own pros and cons. Adding a new one might make this even more complicated and cluttered.
Please, give your thoughts :)
Do not hesitate to give your thoughts about this proposal, whether you think it might be useful or not for the language, discuss about pros and cons!