Hans Hans - 7 months ago 15
Javascript Question

Check if a number has a decimal place/is a whole number

I am looking for an easy way in JavaScript to check if a number has a decimal place in it (is an integer). For instance,

23 -> OK
5 -> OK
3.5 -> not OK
34.345 -> not OK


if(number is integer) {...}

Answer

Using modulus will work:

num % 1 != 0
// 23.5 % 1 = 0.5