29

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?

Zuul
  • 16,217
  • 6
  • 61
  • 88
jirkap
  • 967
  • 2
  • 9
  • 19
  • possible duplicate of [How to check if a variable is an integer in Javascript?](http://stackoverflow.com/questions/14636536/how-to-check-if-a-variable-is-an-integer-in-javascript) – Artjom B. Jun 27 '15 at 21:44

8 Answers8

61
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.

Christoph
  • 164,997
  • 36
  • 182
  • 240
11

This works:

if (Math.floor(x) == x)
Paul D. Waite
  • 96,640
  • 56
  • 199
  • 270
Randy Proctor
  • 7,396
  • 3
  • 25
  • 26
5

You could use the formal definition of integer:

Math.floor(x) === x
2

How about this:

if((typeof(no)=='number') && (no.toString().indexOf('.')==-1))
Johnno Nolan
  • 29,228
  • 19
  • 111
  • 160
  • 1
    That probably depends on your definition (and use case) of integer – bdukes Aug 24 '09 at 16:48
  • Hmm. True. For inherently mathematical questions, I tend to think of them in terms of numbers and not numerals - hence, my comment above. – Matt Ball Aug 24 '09 at 16:53
  • @MattBall It works for `8.000` too. When you invoke `toString()` on that number, you get `'8'`. Demo: http://jsfiddle.net/Msyrh/ – Šime Vidas Oct 21 '11 at 20:45
2

Would this not work:

if (parseInt(number, 10) == number)
{
  alert(number + " is an integer.");
}
paracycle
  • 7,665
  • 1
  • 30
  • 34
  • 1
    You should always include the radix parameter: parseInt(number, 10) – Greg Aug 24 '09 at 16:27
  • @Greg: not necessarily - one might want hexadecimals to pass; octals will fail as they are recognized by `parseInt()` but not by `Number()` – Christoph Aug 24 '09 at 16:55
2

Can use the function Number.isInteger(). It also takes care of checking that the value is a number.

For example:

Number.isInteger(3)     // true
Number.isInteger(3.1)   // false
Number.isInteger('3')   // false

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isInteger

polo-language
  • 826
  • 5
  • 13
0

you could either make use of javas parsing capabilities or as well try out the modulo operator %...

Gnark
  • 4,080
  • 7
  • 33
  • 44
-1

There is a javascript function called isNaN(val) which returns true if val is not a number.

If you want to use val as a number, you need to cast using parseInt() or parseFloat()

EDIT: oops. Corrected the error as mentioned in the comment

David
  • 5,356
  • 2
  • 26
  • 39
  • 1
    Just from the name of the function, I would think "isNaN" would return true if val "is *Not* a Number", which is what NaN stands for. Typo? – Matt Ball Aug 24 '09 at 16:23
  • 1
    isNaN(val) returns true if val is not a number, but it returns false for non-integer numbers as well so it won't solve the problem. – Amok Aug 24 '09 at 16:33