Check if a value is an integer

There are a few different ways you can determine if a value is an integer in JavaScript. ECMAScript6 provides a direct isInteger method, but for current browsers you can apply a few different methods. The simplest is to compare the result of Math.round to the original value. Using triple equality checking also handles non-numbers as well.

function isInteger(x) {
    return Math.round(x) === x;
}
Published: 5/27/2014
Author: Dr. Axel Rauschmayer
Source: http://www.2ality.com/2014/05/is-integer.html
Tags: math
comments powered by Disqus