Why does parseInt("-1000-500-75-33")
return -1000
?
Shouldn't it return the sum of those numbers: -1608
How can I get the string "-1000-500-75-33"
to return as the sum of those numbers?
parseInt
will try to get a number starting from the beginning of the string.
Since -
is a valid character to begin a number with, it parses the string until it finds something invalid. The second -
is invalid because no integer can contain an -
inside it, only digits. So it stops there and considers the number to be "finished".
Now, if you want to process the expression, you can use eval
like so:
eval("-1000-500-75-33")
This will return -1608
as expected.