Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

parseInt and negative zero in javascript: odd behavior?

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?

like image 775
Semicolon Avatar asked Jun 20 '26 04:06

Semicolon


1 Answers

parseInt() works on strings. Running parseInt() on a number causes it to be cast to a string.

(-0).toString() is 0.

like image 183
Brad Avatar answered Jun 21 '26 16:06

Brad