console.log(+true);
console.log(+"");
console.log(+null);
console.log(+undefined);0
What will be the output of the following code, and why does the unary plus operator behave this way in JavaScript?
subina kallyani
hard
4completed
115
Answer
1
0
0
NaNThe unary plus operator (+) attempts to convert its operand into a number:
+true→1
Booleantrueis converted to number1.+""→0
Empty string is converted to0.+null→0
nullis converted to0.+undefined→NaN
undefinedcannot be converted into a meaningful number, resulting inNaN.
Click to Reveal Answer
Tap anywhere to see the solution
Revealed
Comments0