parseInt()
works on strings. Running parseInt()
on a number causes it to be cast to a string.
(-0).toString()
is 0
.
Question
Why:
parseInt(-0, 10) // 0
parseInt('-0', 10) // -0
Is there a sensible reason that parseInt wouldn't do -0 for both or is this just an oddity of javascript?
Solution
parseInt()
works on strings. Running parseInt()
on a number causes it to be cast to a string.
(-0).toString()
is 0
.
OTHER TIPS
The first parameter to the parseInt
is converted to a String, as per ECMA 5.1 Spec for parseInt
,
1. Let inputString be ToString(string).
And ToString
, returns '0'
, even if the input is +0
or -0
,
2. If m is +0 or −0, return the String "0".
That is why you are getting this behavior.