ECMASCRIPT: Optional Chaining | Stage 3

numtostr

Vikas Raj

Posted on July 26, 2019

ECMASCRIPT: Optional Chaining | Stage 3

Optional chaining is at stage 3 🎉🎉. This one is my favourite. Soon will be available in TypeScript.

Optional Chaining for JavaScript

Status

ECMAScript proposal at stage 4 of the process.

Authors

Overview and motivation

When looking for a property value that's deep in a tree-like structure, one often has to check whether intermediate nodes exist:

var street = user.address && user.address.street;
Enter fullscreen mode Exit fullscreen mode

Also, many API return either an object or null/undefined, and one may want to extract a property from the result only when it is not null:

var fooInput = myForm.querySelector('input[name=foo]')
var fooValue = fooInput ? fooInput.value : undefined
Enter fullscreen mode Exit fullscreen mode

The Optional Chaining Operator allows a developer to handle many of those cases without repeating themselves and/or assigning intermediate results in temporary variables:

var street = user.
…
Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
numtostr
Vikas Raj

Posted on July 26, 2019

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related