Patternite Logo

Accessing nested properties without validating each level in JavaScript, ES2020


We can easily access deeply nested object properties using the optional chaining operator (?.), without having to validate each level of the hierarchy


Accessing deeply nested object properties can be verbose, usually requiring multiple levels of the object hierarchy to be validated, as in lines #16 and #21. ES2020 introduces the optional chaining operator ?., which simplifies accessing deeply nested object properties by short circuiting if a property on a nullish value is accessed, rather than throwing an error.

The MDN docs have a good overview of optional chaining.

Profile picture for duncster

| edited

Patternite © 2022

Patternite Logo