+ 4

Nullish coalescing

What is the output of below code : let firstName; let lastName; let nickName = ‘Wolverine’; console.log(firstName ?? lastName ?? nickName ?? ‘Arshad khan’);

29th Jan 2022, 12:04 AM
arshad khan
arshad khan - avatar
6 Answers
+ 2
not the same result if the condition returns an empty string '' or 0. It returns the left-hand side instead. const foo = '' ?? 'default string'; console.log(foo); // output: '' const boo = '' || 'default string'; console.log(boo) // output: 'default string' const baz = 0 ?? 42; console.log(baz); // output: 0 const bar = 0 || 42 console.log(bar) // output: 42 https://code.sololearn.com/cmGho9jKf8r0/?ref=app
30th Jan 2022, 8:32 AM
Bob_Li
Bob_Li - avatar
+ 2
Solomoni Railoa But we can achieve the same using || operator.
29th Jan 2022, 1:05 AM
arshad khan
arshad khan - avatar
+ 1
29th Jan 2022, 9:03 AM
arshad khan
arshad khan - avatar
+ 1
Bob_Li goog explanation.
30th Jan 2022, 4:53 PM
arshad khan
arshad khan - avatar