1Optional chaining: (?.)
2
3The optional chaining operator (?.) enables you to
4read the value of a property located deep within a
5chain of connected objects without having to
6check that each reference in the chain is valid.
7
8The ?. operator is like the . chaining operator,
9except that instead of causing an error if a reference
10is nullish (null or undefined), the expression returns
11a value of undefined.
12
13When used with function calls,
14it returns undefined if the given function does not exist.
15
16Example:
17
18const adventurer = {
19 name: 'Alice',
20 cat: {
21 name: 'Dinah'
22 }
23};
24
25const dogName = adventurer.dog?.name;
26console.log(dogName);
27// output: undefined
28
29console.log(adventurer.someNonExistentMethod?.());
30// output: undefined