In PHP, it’s pretty easy:
is_numeric(23);//true is_numeric('23');//true is_numeric(23.5);//true is_numeric(true);//false
But how do I do this in Javascript? I could use a regular expression, but is there a function for this?
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.
What about:
The isNaN built-in function is used to check if a value is not a number.
Update: Christoph is right, in JavaScript Boolean types are convertible to Number, returning the 1 for true and 0 for false, so if you evaluate
1 + truethe result will be 2.Considering this behavior I’ve updated the function to prevent converting boolean values to its numeric representation.