JavaScript Optional Chaning ( ?. )
Example
// Create an object:
const car = {type:"Fiat", model:"500", color:"white"};
// Ask for car name:
document.getElementById("demo").innerHTML = car?.name;
Try it Yourself »
Description
The Optional Chaning ( ?. ) Operator returns undefined
if an object is
undefined
or null
(instead of throwing an error).
Browser Support
Optional Chaning
is a JavaScript 2020 feature.
ES 2020 is supported in all modern browsers since September 2020:
Chrome 85 |
Edge 85 |
Firefox 79 |
Safari 14 |
Opera 71 |
Aug 2020 | Aug 2020 | Mar 2020 | Sep 2020 | Sep 2020 |