4 ms·
Except, not "everyone" would expect that. I'd expect "hello" - 1 to return NaN, since you can't perform numerical subtraction on something that isn't a number.
by rprospero 15y ago
Except, not "everyone" would expect that.
I'd expect "hello" - 1 to return NaN, since you can't perform numerical subtraction on something that isn't a number. That's exactly what Javascript does.
In the same way, I'd expect "hello" + 1 to return Nan, since you can't perform string concatenation on something that isn't a string, and you can't perform numerical addition on something that isn't a number.
Edited for clarity
- maximusprime 15y ago"hello" - 1 doesn't return NaN out of some sense of exception to trying to subtract from a string. It returns it because parseFloat("hello") = NaN, and NaN - 1 = NaN.