Skip to content

Instantly share code, notes, and snippets.

@hvitis
Created January 11, 2022 10:08
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 hvitis/68cfb6bc7fbc06b64a2f19dac36e566d to your computer and use it in GitHub Desktop.
Save hvitis/68cfb6bc7fbc06b64a2f19dac36e566d to your computer and use it in GitHub Desktop.
πŸ”₯ JavaScript Syntactic Sugar 🍭
// 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!
// 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