Skip to content

Instantly share code, notes, and snippets.

@AbdelrahmanHafez
Last active October 2, 2019 05:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AbdelrahmanHafez/84b23ee7268e45c4a2277cf0c7091c5e to your computer and use it in GitHub Desktop.
Save AbdelrahmanHafez/84b23ee7268e45c4a2277cf0c7091c5e to your computer and use it in GitHub Desktop.
Medium optional chaining example
// 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