HardType Conversion💻 Output Question

Implicit Numeric Conversion

💡

Hint

Pay attention to how different data types are converted to numbers and how strings are parsed

What does this output?

console.log(Number(undefined));
console.log(Number(null));
console.log(Number(true));
console.log(Number(false));
console.log(Number('   123   '));
console.log(Number('123abc'));
console.log(Number('   '));

Correct Output

NaN
0
1
0
123
NaN
0

Why this output?

The code demonstrates implicit numeric conversion using the Number() function. The conversion rules are as follows: undefined becomes NaN, null becomes 0, true becomes 1, false becomes 0. For strings, leading and trailing whitespaces are removed. If the resulting string is empty, the result is 0. If the string can be parsed as a number, the number is 'read' from the string. Otherwise, the result is NaN. Therefore, Number(undefined) is NaN, Number(null) is 0, Number(true) is 1, Number(false) is 0, Number(' 123 ') is 123, Number('123abc') is NaN because '123abc' cannot be parsed as a number, and Number(' ') is 0 because the string is empty after removing whitespaces.

Practice predicting output live →

66 output questions with instant feedback

💻 Try Output Quiz