Created
January 11, 2022 10:08
-
-
Save hvitis/68cfb6bc7fbc06b64a2f19dac36e566d to your computer and use it in GitHub Desktop.
π₯ JavaScript Syntactic Sugar π
This file contains hidden or 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
// Nullish coalescing operator with conditional chaining | |
let alien = { | |
isAware: true, | |
location : { | |
planet: "C435", | |
distance: 30 // light years | |
} | |
}; | |
const alienGreeting = alien?.name ?? "Welcome Stranger!"; | |
console.log(alienGreeting); // Welcome Stranger! |
This file contains hidden or 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
// Stacking the optional chaining operator | |
let alien = { | |
isAware: true, | |
location : { | |
planet: "C435", | |
distance: 30 // light years | |
}, | |
travel: flyTheShip() | |
}; | |
let travelLocation = alien.location?.distance; // true | |
let travelLocation = alien.location?.galaxy?.name; // false | |
// It's also possible to evoke method while chaining | |
let galaxyName = alien.location?.galaxy?.getName.(); // method does not exist, galaxyName is undefined |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment