This website collects cookies to deliver better user experience
Optional Chaining Operator (?.) - JavaScript
Optional Chaining Operator (?.) - JavaScript
Topics covered:
1- Intro
2- Optional Chaining with Functions
3- Optional Chaining with Expressions
4- Optional Chaining with Arrays
5- With Nullish Operator
1- Intro
Optional chaining operator ?. loves you to read the value of within objects / nested objects with safety.
?. is similar to . except if the property in a object does not exist while accessing, it will return undefined instead of error/ breaking the program. This means you can explore The object with the carefree mind whether the property exists or not, thus making your code more solid.
const person ={name:'John',dog:{name:'toto'}}const catName = person.cat.name// errorconst catName = person.cat?.name // error-free line (undefined)