Never knew about this one! Quite funny actually and I'm curious why that happens. I suppose because `0.0000001` is represented as an exponent rather than a decimal? Although I haven't seen `parseInt` used since 2015, you should use `Number`.
> "" + 5 == "5"
> "" - 5 = -5
iirc
`+` operator:
- If LHS is a string, concatenate
- Otherwise, cast to Number and perform arithmetic.
`-` operator:
- Perform arithmetic.
All of these are explainable, and never catch anybody competent out in practice. And, since TypeScript is the norm in a lot of places now, it's never an issue.
parseInt wants a string. If the input isn't a string, it calls toString. 0.0000001.toString() gives "1e-7". parseInt takes the first number it seems in a string, so 1 in this case, or like parseInt("123asdfasdf") gives 123.
All of these are cause JS builtins like to auto-cast things. + only accepts strings or numbers, - only accepts numbers. Really these should be errors instead, but it's also not very surprising or annoying once you know this. Basically, don't perform arithmetic on things that aren't numbers.
The name parseInt suggests it takes a string. Especially "" - 5, why?
Well, `+` is a string operator. There's no infix array concatenation operator, so makes sense.
> parseInt(0.000001) == 0 > parseInt(0.0000001) == 1
Never knew about this one! Quite funny actually and I'm curious why that happens. I suppose because `0.0000001` is represented as an exponent rather than a decimal? Although I haven't seen `parseInt` used since 2015, you should use `Number`.
> "" + 5 == "5" > "" - 5 = -5
iirc
`+` operator: - If LHS is a string, concatenate - Otherwise, cast to Number and perform arithmetic.
`-` operator: - Perform arithmetic.
All of these are explainable, and never catch anybody competent out in practice. And, since TypeScript is the norm in a lot of places now, it's never an issue.