JavaScript

値が整数かチェックする

More than 3 years have passed since last update.

メモ

function isInteger(x) {

return Math.round(x) === x;
}

> isInteger(1)
true
> isInteger(0)
true
> isInteger(1.111)
false
> isInteger("1")
false
> isInteger("")
false
> isInteger(null)
false
> isInteger(NaN)
false
> isInteger(undefined)
false

http://www.2ality.com/2014/05/is-integer.html