Created
April 2, 2020 05:53
-
-
Save fczbkk/75996d313e0398fb8d16fe968e3e3405 to your computer and use it in GitHub Desktop.
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
// 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