Nullish coalescing operator '??' Flashcards
_____ returns the first argument if it’s not null/undefined. Otherwise, the second one.
//
nullish coalescing operator
nullish coalescing operator returns?
first agrument if it’s not null or undefined
let user;
alert(user ?? “Anonymous”); //
returns and why?
Anonymous
because user is undefined
let user = “John”;
alert(user ?? “Anonymous”); //
returns and why?
John
(user is not null/undefined)
let user = “John”;
alert(“Anonymous” ?? user);
// returns and why
undefined
first argument is “undefined”
let firstName = null;
let lastName = null;
let nickName = “Supercoder”;
alert(firstName ?? lastName ?? nickName ?? “Anonymous”);
// returns and why
Supercoder
firstname and lastname is null
?? returns the first ______ value
defined
|| returns the first ______ value.
truthy
let height = 0;
alert(height || 100);
// returns and why
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.
let height = 0;
alert(height ?? 100);
// returns and why?
0
The height ?? 100 checks height for being null/undefined, and it’s not, so the result is height “as is”, that is 0
The ______ provides a short way to choose the first “defined” value from a list.
nullish coalescing operator ??
let x = 1 && 2 ?? 3;
// the above give a Syntax error how can you fix?
need parentheses