Skip to content

TC39 proposal for a "not nullish" `?` operator

License

Notifications You must be signed in to change notification settings

clemdee/proposal-not-nullish-operator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Non-nullish ? Operator

Champion: looking for one :)

Author: @ClemDee

Status: 0 (strawperson)

Original TC39 thread

Motivation

Over the past few years, several proposals have been made to ease the handling of nullish values:

  • the optional chaining operator ?.
  • the nullish coalescing operator ??
  • the logical nullish assignment ??=

However, a use case is still missing, and yet, a very simple and common one: checking whether a variable is nullish or not.

For now, we would still have to write:

if (variableA !== undefined && variableA !== null) {
 // ...
}

Or using the loose equality:

if (variableA != null) {
  // ...
}

However, using loose equality is considered as a bad practise, as it is one of the most awkward feature of javascript. Even though linters can be configured to only allow loose equality when comparing with null (and is a commonly used exception to this syntax), this feels "hack-y", for lack of better.

Adding a dedicated syntax for this use case would also make the language less confusing for new-commers, and could hopefully lead to banning loose equality for good.

Proposal

The goal of the proposal is to add the non-nullish ? unary operator:

if (?variableA) {
  // do something if variableA is not nullish
}

This could desugar to something like:

function isDefined (variable) {
  return variable !== undefined && variable !== null;
}

if (isDefined(variableA)) {
  // do something if variableA is not nullish
  // note that variableA was only evaluated once
}

This new operator would improve both writability and readability compared to today available solutions.

Example use with other operators

This operator combines really well with other operators of the langage:

  • We can easily evaluate if a variable is nullish, combining this operator with the logical not ! operator:
if (!?variableA) {
  // do something if variableA is nullish
}
  • This operator also goes really well along with the optional chainning ?. operator:
if (?myObj?.sub?.value) {
  // do something if myObj, sub and value are not nullish
}

Operator precedence

The operator precedence should be at the same level than the logical not operator (level 17), with left-to-right associativity.

Also, the ? operator cannot be applied twice wihout parentheses or whitespace, as it would form a nullish coalescing ?? operator, and thus throw a syntax error:

if (??variableA) // Syntax Error
if (?(?variableA)) // OK
if (? ?variableA) // OK

Note that applying this operator several times would not be very relevant, as it would always return true at the end.

Handling undeclared variables

The ? operator should not handle undeclared variables (like when using the typeof syntax). Like other nullish operators, it should throw a ReferenceError exception if the variable is not declared.

Backward compatibility

The proposed ? operator should not break any previous code.

While a ? operator is already used for ternary condition condition ? ifTrue : ifFalse, as its syntax expects 3 members and a second : operator, there is not risk that it would be mistaken by Javascript engines / parsers.

Considered alternatives

Using a postfix operator

The idea of using a posfix operator was discussed, like the existential operator in Coffeescript. However, most operators in Javascript are prefix, and having this one operator being suffix would lead to awkward syntaxes.
E.g. when checking if a variable is nullish:

if (!variableA?)
// compared to the prefix version:
if (!?variableA)

Also, the suffix version would be closer to the syntax of ternary conditions, potentially introducing breaking changes.

Concerns

Yet another way of checking undefined variables?

Indeed, there are already too many ways of checking for undefined variables. This proposal aims to become the preferred way to handle both undefined and null values, in a ES6+ spirit.
This could replace almost every existing ways of checking undefined:

  • variableA !== undefined && variableA !== null
  • variableA != null
  • variableA !== undefined
  • variableA !== void 0

But as it is not handling undeclared variables, it would not replace:

  • typeof undeclaredVariable === "undefined"

About

TC39 proposal for a "not nullish" `?` operator

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages