Skip to content

Instantly share code, notes, and snippets.

@yano3nora
Created August 24, 2020 11:39
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 yano3nora/78e0777708af7d57bb31bedae093bce1 to your computer and use it in GitHub Desktop.
Save yano3nora/78e0777708af7d57bb31bedae093bce1 to your computer and use it in GitHub Desktop.
[js: Optional Chaining] いわゆる null セーフな . チェイン。 #js
// IE 非対応なので polyfill or babel で頑張る必要ある
// https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Operators/Optional_chaining
const adventurer = {
name: 'Alice',
cat: {
name: 'Dinah'
}
};
const dogName = adventurer.dog?.name;
console.log(dogName);
// expected output: undefined
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment