Optional Chaining for JavaScript
Status
ECMAScript proposal at stage 4 of the process.
Authors
- Claude Pache (github)
- Gabriel Isenberg (github, twitter)
- Daniel Rosenwasser (github, twitter)
- Dustin Savery (github, twitter)
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;
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
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.
…