Skip to content

Instantly share code, notes, and snippets.

@thawkin3
Last active March 4, 2020 19:43
Show Gist options
  • Select an option

  • Save thawkin3/91e89ad6e1963a4f0bc4a40c2d19d03d to your computer and use it in GitHub Desktop.

Select an option

Save thawkin3/91e89ad6e1963a4f0bc4a40c2d19d03d to your computer and use it in GitHub Desktop.
Accessing deeply nested object properties with the optional chaining operator
const user = {
firstName: 'John',
lastName: 'Doe',
address: {
street: '123 Anywhere Lane',
city: 'Some Town',
state: 'NY',
zip: 12345,
},
}
const street = user?.address?.street
// '123 Anywhere Lane'
const badProp = user?.fakeProp?.fakePropChild
// undefined
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment