Skip to content

Latest commit

 

History

History
28 lines (22 loc) · 885 Bytes

102-optional-chaining.md

File metadata and controls

28 lines (22 loc) · 885 Bytes
title description weight lastmod draft vimeo emoji video_length quiz
Optional Chaining
Call object properties safely
23
2022-11-11 10:23:30 -0900
false
773489916
6360
true
What is the return value when calling a property that does not exist with optional chaining?

Optional Chaining

Optional chaining ? is a relatively new operator that was introduced in ES2020. It allows you to call object properties safely, without throwing an error. When calling properties without this operator, you many crash your applcation with the error Cannot read property 'foo' of undefined.

const person = { };

const dude = person.name;
console.log(foo); // Uncaught TypeError: Cannot read property 'bar' of undefined

const dude = person?.name; // undefined