Answers for "optional chaining"

4

optional chaining

let myMap = new Map();
myMap.set("foo", {name: "baz", desc: "inga"});

let nameBar = myMap.get("bar")?.name;
Posted by: Guest on February-25-2020
2

optional chain operator

const adventurer = {
  name: 'Alice',
  cat: {
    name: 'Dinah'
  }
};

const dogName = adventurer.dog?.name;
console.log(dogName);
// expected output: undefined
Posted by: Guest on October-12-2020
2

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];
}
Posted by: Guest on March-02-2020
0

optional chaining

const greeting = object?.deepProp?.deeperProp?.greet
Posted by: Guest on June-25-2021
0

Optional chaining

let x = foo?.bar();

if (foo?.bar?.baz) { // ... }
Posted by: Guest on June-04-2021
-1

optional chaining

const array = [1,2,3,4,5];
let arrItem = array?.[4]; 

console.log(arrItem); /// 5
Posted by: Guest on October-09-2020

Code answers related to "Javascript"

Browse Popular Code Answers by Language