Chooce video playback speed
speed:1

Access Long Chains of Properties with JavaScript Optional Chaining

InstructorShruti Kapoor

Share this video with your friends

Send Tweet

Accessing a long chain of properties can be risky since one or more properties could be undefined or null. To be safe, we can use optional chaining when accessing long chain of properties.

This lesson is a Community Resource

A Community Resource means that it’s free to access for all. The instructor of this lesson requested it to be open to the public.

Instructor: Optional chaining can be used to chain properties that maybe now are undefined. It is especially useful for accessing a long chain of properties. like here.

Consider this example, there are long chains being accessed, and there are multiple points of failure in the statement. Any of the properties in this long chain could be null or undefined. Then, we try to access them. JavaScript then throw an error.

To fix this, we can use optional chaining while accessing properties, and now JavaScript returns undefined if any one of these properties is null or undefined.