Optional chaining '?.' Flashcards
let user = {};
alert(user.address.street);
// Error!
what to use then rewrite code:
alert( )
Optional chaining
alert( user?.address?.street ); // undefined (no error)
let user = null;
let x = 0;
user?.sayHi(x++); // returns?
alert(x); // returns?
no “user”, so the execution doesn’t reach
0, value not incremented
let user = null;
user?.name = “John”; // returns and why?
Error, doesn’t work because it evaluates to: undefined = “John”
The optional chaining ?. has no use on the______
left side of an assignment.
obj?.prop – returns //?
obj.prop if obj exists, otherwise undefined.
obj?.[prop] – returns // ?
obj[prop] if obj exists, otherwise undefined.
obj.method?.() – // returns?
calls obj.method() if obj.method exists, otherwise returns undefined.