Last active
October 2, 2019 05:17
-
-
Save AbdelrahmanHafez/84b23ee7268e45c4a2277cf0c7091c5e to your computer and use it in GitHub Desktop.
Medium optional chaining example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// When you have a deeply nested object like that | |
// *and you're not sure* whether or not a property exists | |
const user = { | |
address: { | |
city: { | |
name: 'Tokyo' | |
} | |
} | |
}; | |
// pre-optional-chaining | |
const name = user.address && user.address.city && user.address.city.name; | |
// with optional chaining | |
const name = user?.address?.city?.name; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment