r/ProgrammerHumor • • Dec 31 '14

How to add two numbers in JavaScript

1.7k Upvotes

270 comments sorted by

View all comments

Show parent comments

2

u/Hydrothermal Dec 31 '14

Dropping the radix argument to parseInt works until your number starts with a 0. Then you end up with this.

2

u/mvaneerde Dec 31 '14

Where did the 08 come from? Are you parsing a date manually?

1

u/Hydrothermal Dec 31 '14

I've never personally had this problem. I imagine somebody trying to parse user input could end up with it.

1

u/the_omega99 Dec 31 '14

Presumably. That, or a time.

Questionable why they'd do that, since the Date object can do better here, but not necessarily bad practice (assuming that you know the date format won't change).

1

u/wordsnerd Dec 31 '14

If you can ignore IE8 and below (almost all greenfield work IMHO), it's safe to assume parseInt defaults to decimal even with the leading 0.