Optional chaining '?.' Flashcards

1
Q

let user = {};
alert(user.address.street);
// Error!

what to use then rewrite code:
alert( )

A

Optional chaining

alert( user?.address?.street ); // undefined (no error)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

let user = null;
let x = 0;

user?.sayHi(x++); // returns?

alert(x); // returns?

A

no “user”, so the execution doesn’t reach

0, value not incremented

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

let user = null;

user?.name = “John”; // returns and why?

A

Error, doesn’t work because it evaluates to: undefined = “John”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

The optional chaining ?. has no use on the______

A

left side of an assignment.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

obj?.prop – returns //?

A

obj.prop if obj exists, otherwise undefined.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

obj?.[prop] – returns // ?

A

obj[prop] if obj exists, otherwise undefined.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

obj.method?.() – // returns?

A

calls obj.method() if obj.method exists, otherwise returns undefined.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly