optional chaining
let myMap = new Map();
myMap.set("foo", {name: "baz", desc: "inga"});
let nameBar = myMap.get("bar")?.name;
optional chaining
let myMap = new Map();
myMap.set("foo", {name: "baz", desc: "inga"});
let nameBar = myMap.get("bar")?.name;
optional chain operator
const adventurer = {
name: 'Alice',
cat: {
name: 'Dinah'
}
};
const dogName = adventurer.dog?.name;
console.log(dogName);
// expected output: undefined
optional chaining
/*
* optional chaining (?.) allows me to write code that stops
* running when we encounter a null or undefined value
*/
function tryGetFirstElement<T>(arr?: T[]) {
return arr?.[0];
// equivalent to
// return (arr === null || arr === undefined) ?
// undefined :
// arr[0];
}
optional chaining
const greeting = object?.deepProp?.deeperProp?.greet
Optional chaining
let x = foo?.bar();
if (foo?.bar?.baz) { // ... }
optional chaining
const array = [1,2,3,4,5];
let arrItem = array?.[4];
console.log(arrItem); /// 5
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us