In JavaScript, why is:
new String * 1
<- 0
What exactly makes it return 0
, and why is the following equal to NaN
?
function Foo() { this.bar = 0; }
new Foo * 5;
<- NaN
If you use arithmetic operators like *
, JavaScript will try to convert the type to a number. An empty string becomes 0
.
If you have, for example:
new String("foo") * 1
You will notice it returns NaN
because the conversion to a number could not be completed. That's what happens in your second situation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With