javascript
Optional Chaining on Javascript Object
The optional chaining can be implemented using ?. It stops evaluation of expression after ? sign. It will return undefined if the expression retuns undefined before the ? sign.
const user = {
admin: {
name: "John Deo"
}
};
user.admin?.name;
// "John Deo"
user.admi?.name;
// undefined
user.admi.name
// TypeError: Cannot read property 'name' of undefined
Was this helpful?
Similar Posts
- Promise Chaining
- Loop through object javascript
- JavaScript ES6 filtering object with multiple params
- Convert object {key: value} to Array [[key, value]] Javascript
- Get all keys from an object using Javascript
- Get all values as array items from an Object in Javascript
- Javascript to convert all object keys to lowercase