MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/ProgrammerHumor/comments/2qwnus/how_to_add_two_numbers_in_javascript/cnacxj1
r/ProgrammerHumor • u/tender_breasts • Dec 31 '14
270 comments sorted by
View all comments
Show parent comments
2
Dropping the radix argument to parseInt works until your number starts with a 0. Then you end up with this.
parseInt
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.
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
I've never personally had this problem. I imagine somebody trying to parse user input could end up with it.
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).
Date
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.
2
u/Hydrothermal Dec 31 '14
Dropping the radix argument to
parseIntworks until your number starts with a 0. Then you end up with this.