Skip to content

Instantly share code, notes, and snippets.

@fczbkk
Created April 2, 2020 05:53
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 fczbkk/75996d313e0398fb8d16fe968e3e3405 to your computer and use it in GitHub Desktop.
Save fczbkk/75996d313e0398fb8d16fe968e3e3405 to your computer and use it in GitHub Desktop.
// môže hodiť chybu, ak chýba `data`, `user` alebo `address`
const city = data.user.address.city
// relatívne bezpečné, ale strašne hnusné
const city = data &&
data.user &&
data.user.address &&
data.user.address.city
// ešte bezpečnejšie, ale o to hnusnejšie
const city = (
data
? (
data.user
? (
data.user.address
? data.user.address.city
: undefined
)
: undefined
)
: undefined
)
// optional chaining, bezpečné a nádherné
const city = data?.user?.address?.city
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment