← Today I Learned
- development
- javascript
Aug 10, 2020 @ 6:35 AM
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"];