How can I test a variable to ascertain if it contains a number, and it is an integer?
e.g.
if (1.589 == integer) // false
if (2 == integer) // true
Any clues?
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.
This will convert
numto typeNumberfirst, so any value which can be converted to an integer will pass the test (e.g.'42',true).If you want to exclude these, additionally check for
You could also use
parseInt()to do this, iefor an untyped check and
for a typed check.
Note that the tests are not equivalent: Checking via
parseInt()will first convert toString, so egtruewon’t pass the check.Also note that the untyped check via
parseInt()will handle hexadecimal strings correctly, but will fail for octals (ie numeric strings with leading zero) as these are recognized byparseInt()but not byNumber(). If you need to handle decimal strings with leading zeros, you’ll have to specify the radix argument.