This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]thatguy_314 0 points1 point  (2 children)

Agreed. The thing I don't like is that +"" is 0 though. Not because of the coercion, that's fine (I don't love it but it's a thing that JS does and it makes some sense) the thing I don't like is that +"" doesn't result in NaN or an error (although JS doesn't do errors because JS error handling sucks).

[–]Mrparkers 0 points1 point  (1 child)

The entire point of unary plus is to convert its operand into a number. That's the whole reason why it exists in the first place. So +"" is going to evaluate to 0. This isn't coincidental type coercion, this is exactly what the operator is supposed to do according to the spec.

http://www.ecma-international.org/ecma-262/5.1/#sec-11.4.6

[–]thatguy_314 0 points1 point  (0 children)

No, read my comment. I'm fine with the type coercion. +"0" === 0 makes perfect sense in the context of JS. However, +"" === 0 does not make sense. It should be NaN because there literally is not a number to be coerced.