Join the AI Workshop to learn more about AI and how it can be applied to web development. Next cohort February 1st, 2026
The AI-first Web Development BOOTCAMP cohort starts February 24th, 2026. 10 weeks of intensive training and hands-on projects.
JavaScript provides several methods to validate numbers and check their properties.
Number.isNaN()
NaN is a special case. A number is NaN only if it’s NaN or if it’s a division of 0 by 0 expression, which returns NaN. In all the other cases, we can pass it what we want but it will return false:
Number.isNaN(NaN) //true
Number.isNaN(0 / 0) //true
Number.isNaN(1) //false
Number.isNaN('Flavio') //false
Number.isNaN(true) //false
Number.isNaN({}) //false
Number.isNaN([1, 2, 3]) //false
Number.isFinite()
Returns true if the passed value is a finite number. Anything else, booleans, strings, objects, arrays, returns false:
Number.isFinite(1) //true
Number.isFinite(-237) //true
Number.isFinite(0) //true
Number.isFinite(0.2) //true
Number.isFinite('Flavio') //false
Number.isFinite(true) //false
Number.isFinite({}) //false
Number.isFinite([1, 2, 3]) //false
Number.isInteger()
Returns true if the passed value is an integer. Anything else, booleans, strings, objects, arrays, returns false:
Number.isInteger(1) //true
Number.isInteger(-237) //true
Number.isInteger(0) //true
Number.isInteger(0.2) //false
Number.isInteger('Flavio') //false
Number.isInteger(true) //false
Number.isInteger({}) //false
Number.isInteger([1, 2, 3]) //false
Number.isSafeInteger()
A number might satisfy Number.isInteger() but not Number.isSafeInteger() if it goes out of the boundaries of safe integers.
So, anything over 2^53 and below -2^53 is not safe:
Number.isSafeInteger(Math.pow(2, 53)) // false
Number.isSafeInteger(Math.pow(2, 53) - 1) // true
Number.isSafeInteger(Math.pow(2, 53) + 1) // false
Number.isSafeInteger(-Math.pow(2, 53)) // false
Number.isSafeInteger(-Math.pow(2, 53) - 1) // false
Number.isSafeInteger(-Math.pow(2, 53) + 1) // true
Checking if a Value is a Number
We have various ways to check if a value is a number.
The first is isNaN(), a global variable, assigned to the window object in the browser:
const value = 2
isNaN(value) //false
isNaN('test') //true
isNaN({}) //true
isNaN(1.2) //false
If isNaN() returns false, the value is a number.
Another way is to use the typeof operator. It returns the 'number' string if you use it on a number value:
typeof 1 //'number'
const value = 2
typeof value //'number'
So you can do a conditional check like this:
const value = 2
if (typeof value === 'number') {
//it's a number
}