If I write this script :
alert(parseInt("123blahblahblah456"));
I get the alert with the value 123
Ideally, shouldn’t the function NOT do anything since it is an invalid integer string?
Similar is the case with parseFloat()
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.
Yes:
parseInt()is absolutely meant to work like that; to quote the Mozilla Developer Network entry:It seems that
parseInt()is explicitly expecting to take a string and will take the first sequence of numbers (until it encounters an invalid numerical character) and return that as a number of whatever base was specified in the radix parameter.Incidentally, to reduce errors when parsing the strings passed to
parseInt()remember to use the radix parameter, for example:Reference:
parseInt()at the MDC.