How do I check if a variable is an integer in JavaScript, and throw an alert if it isn't? I tried this, but it doesn't work:
<html>
<head>
<script type="text/javascript">
var data = 22;
alert(NaN(data));
</script>
</head>
</html>
Lodash https://lodash.com/docs#isInteger (since 4.0.0) has function to check if variable is an integer:
Ok got minus, cause didn't describe my example, so more examples:):
I use regular expression and test method:
Usage:
or:
Usage:
To check if integer like poster wants:
notice + in front of data (converts string to number), and === for exact.
Here are examples:
First off, NaN is a "number" (yes I know it's weird, just roll with it), and not a "function".
You need to check both if the type of the variable is a number, and to check for integer I would use modulus.