Nullish coalescing operator '??' Flashcards

1
Q

_____ returns the first argument if it’s not null/undefined. Otherwise, the second one.

A

//
nullish coalescing operator

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

nullish coalescing operator returns?

A

first agrument if it’s not null or undefined

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

let user;
alert(user ?? “Anonymous”); //
returns and why?

A

Anonymous
because user is undefined

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

let user = “John”;
alert(user ?? “Anonymous”); //
returns and why?

A

John
(user is not null/undefined)

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

let user = “John”;
alert(“Anonymous” ?? user);
// returns and why

A

undefined
first argument is “undefined”

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

let firstName = null;
let lastName = null;
let nickName = “Supercoder”;

alert(firstName ?? lastName ?? nickName ?? “Anonymous”);
// returns and why

A

Supercoder
firstname and lastname is null

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

?? returns the first ______ value

A

defined

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

|| returns the first ______ value.

A

truthy

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

let height = 0;
alert(height || 100);
// returns and why

A

100

The height || 100 checks height for being a falsy value, and it’s 0, falsy indeed.
so the result of || is the second argument, 100.

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

let height = 0;
alert(height ?? 100);
// returns and why?

A

0

The height ?? 100 checks height for being null/undefined, and it’s not, so the result is height “as is”, that is 0

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

The ______ provides a short way to choose the first “defined” value from a list.

A

nullish coalescing operator ??

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

let x = 1 && 2 ?? 3;
// the above give a Syntax error how can you fix?

A

need parentheses

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