I was working on type conversion of Boolean values to Number in javascript.
console.log(new Number(true));//Prints 1
console.log(+true);//Prints 1
console.log(pa
If you are actually working on number conversions then this table might be very helpful:
Number
constructor invokes toNumber
internally which as per spec
Boolean - Return 1 if argument is true. Return +0 if argument is false.
Which means Number(true)
returns 1.
However, parseInt invokes toString
internally and as per spec
Boolean
If argument is true, return "true".If argument is false, return "false".
Which means parseInt(true)
-> parseInt("true")
-> NaN
since as per spec again
- If number is NaN, +0, −0, +∞, or −∞, return +0.
Hope this helped.
Because parseInt
expects a string and 'true'
is NaN
.
From MDN:
parseInt()
The
parseInt()
function parses a string argument and returns an integer of the specified radix (the base in mathematical numeral systems).