- Home
- /
- Tutorials
- /
- JavaScript Tutorial
- /
- JavaScript Optional Chaining
JavaScript Objects
JavaScript Optional Chaining
JavaScript optional chaining reads a nested property without throwing when part of the path is missing.
It is written as a question mark before the dot.
If anything along the path is missing, the whole expression gives undefined.
Example
Example
javascript
const user = { name: "Ada" };
console.log(typeof user.address?.city);Without the question mark this would throw an error.
With it, the result is undefined.
What is Optional Chaining?
?. checks whether the value before it is null or undefined.
