How to Check If a Variable Is an Integer in JavaScript

How to check if a variable is an integer in JavaScript?

Use the === operator (strict equality) as below,

if (data === parseInt(data, 10))
alert("data is integer")
else
alert("data is not an integer")

Checking if variable is an Integer in Javascript

prompt will always return a string so in your case:

var integerAge = parseInt(age);

if(!isNaN(integerAge) && age === '' + integerAge)
alert("data is integer")
else
alert("data is not an integer")

In the case of an age, you'll also probably check it's a positive integer with some integerAge >= 0 or custom minimum and maximum in the next validation step.

How to detect if a given number is an integer?

num % 1 === 0

This will convert num to type Number first, 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

typeof num === 'number'

You could also use parseInt() to do this, ie

parseInt(num) == num

for an untyped check and

parseInt(num) === num

for a typed check.

Note that the tests are not equivalent: Checking via parseInt() will first convert to String, so eg true won'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 by parseInt() but not by Number(). If you need to handle decimal strings with leading zeros, you'll have to specify the radix argument.

Check whether variable is number or string in JavaScript

If you're dealing with literal notation, and not constructors, you can use typeof:.

typeof "Hello World"; // string
typeof 123; // number

If you're creating numbers and strings via a constructor, such as var foo = new String("foo"), you should keep in mind that typeof may return object for foo.

Perhaps a more foolproof method of checking the type would be to utilize the method found in underscore.js (annotated source can be found here),

var toString = Object.prototype.toString;

_.isString = function (obj) {
return toString.call(obj) == '[object String]';
}

This returns a boolean true for the following:

_.isString("Jonathan"); // true
_.isString(new String("Jonathan")); // true

checking if input is integer using javascript

The value of the #num element is returned as a string as you can see in the console. Just revert it into a number using + sign.

Another thing - you are overwriting the innerHTML attribute of the #num element with every function call. You have to insert the second action document.getElementById("show").innerHTML = "Number is not integer" into the else statement to avoid overwriting.

function myFunction() {  var num = document.getElementById("num").value;  console.log(typeof num);
if (Number.isInteger(+num)) { document.getElementById("show").innerHTML = "Number is integer"; } else { document.getElementById("show").innerHTML = "Number is not integer"; }}
<input id="num"></input><button onclick="myFunction()">Submit</button><p id="show">Result Appears Here</p>

How to check if a variable is an integer in JavaScript?

Use the === operator (strict equality) as below,

if (data === parseInt(data, 10))
alert("data is integer")
else
alert("data is not an integer")

check a string variable for string and Integer in Javascript

if(isNaN(m)) {
//is not a number
} else {
//is a number
}

Should work. NaN == NaN actually returns false.

EDIT: As the comments above said, the parseInt is not actually necessary.



Related Topics



Leave a reply



Submit