I have an annoying problem in JavaScript.
> parseInt(1 / 0, 19)
> 18
Why does the parseInt function return 18?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The result of
1/0isInfinity.parseInttreats its first argument as a string which means first of allInfinity.toString()is called, producing the string"Infinity". So it works the same as if you asked it to convert"Infinity"in base 19 to decimal.Here are the digits in base 19 along with their decimal values:
What happens next is that
parseIntscans the input"Infinity"to find which part of it can be parsed and stops after accepting the firstI(becausenis not a valid digit in base 19).Therefore it behaves as if you called
parseInt("I", 19), which converts to decimal 18 by the table above.