← Today I Learned

Optional chaining objects with bracket notation

  • development
  • javascript
Published

Aug 10, 2020 @ 6:35 AM

Published

Aug 22, 2020 @ 10:13 PM

Overview

Summary

You are probably familiar with this representation of optional chaining:

const nestedProp = obj.first?.second;

if not:

The optional chaining operator (?.) permits reading the value of a property located deep within a chain of connected objects without having to expressly validate that each reference in the chain is valid. | Source

You can use optional chaining with objects that use bracket notation.

Example

const nestedProp = obj?.["first"]?.["second"];

Resources

All rights reserved 2023