Hacker News new | ask | show | jobs
by rprospero 5289 days 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

1 comments

"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.