Number.isNaN() / isFinite() / isInteger() Since: ES6(ECMAScript 2015)
These are functions for determining whether a value is a valid number. Three methods are provided: NaN detection, finite number detection, and integer detection.
Syntax
// Checks whether a value is NaN. var result = Number.isNaN(value); // Checks whether a value is a finite number. var result = Number.isFinite(value); // Checks whether a value is an integer. var result = Number.isInteger(value);
Method List
| Method | Description |
|---|---|
| Number.isNaN(value) | Determines whether the value is NaN. Returns true if it is NaN, or false otherwise. |
| Number.isFinite(value) | Determines whether the value is a finite number. Returns true if the value is a number that is not Infinity, -Infinity, or NaN. |
| Number.isInteger(value) | Determines whether the value is an integer. Returns true if the value has no fractional part. |
Sample Code
// Number.isNaN() returns true only for NaN itself.
console.log(Number.isNaN(NaN)); // Outputs 'true'.
console.log(Number.isNaN(123)); // Outputs 'false'.
console.log(Number.isNaN("abc")); // Outputs 'false'. A string is not NaN.
console.log(Number.isNaN(undefined)); // Outputs 'false'.
// NaN occurs when a calculation fails.
var result = 0 / 0;
console.log(Number.isNaN(result)); // Outputs 'true'.
// Number.isFinite() checks whether the value is a finite number.
console.log(Number.isFinite(42)); // Outputs 'true'.
console.log(Number.isFinite(3.14)); // Outputs 'true'.
console.log(Number.isFinite(Infinity)); // Outputs 'false'.
console.log(Number.isFinite(NaN)); // Outputs 'false'.
console.log(Number.isFinite("42")); // Outputs 'false'. A string is not a numeric type.
// Number.isInteger() checks whether the value is an integer.
console.log(Number.isInteger(10)); // Outputs 'true'.
console.log(Number.isInteger(3.14)); // Outputs 'false'.
console.log(Number.isInteger(10.0)); // Outputs 'true'. Treated as an integer because the fractional part is 0.
Difference Between isNaN() and Number.isNaN()
The global function isNaN() and Number.isNaN() behave differently. isNaN() converts its argument to a number before checking for NaN, which means strings and other non-numeric values can also be judged as true. Use Number.isNaN() for more reliable detection.
// The global isNaN() converts its argument to a number before checking.
console.log(isNaN("abc")); // Outputs 'true'. Because Number("abc") is NaN.
console.log(isNaN("123")); // Outputs 'false'. Because Number("123") is 123.
// Number.isNaN() does not perform type conversion — it checks for NaN itself.
console.log(Number.isNaN("abc")); // Outputs 'false'. A string is not NaN itself.
Overview
Number.isNaN(), Number.isFinite(), and Number.isInteger() are numeric checking methods added in ES6. Because none of them perform type conversion, they always return false when the value is not of the number type. This makes their behavior more predictable than the global isNaN() and isFinite() functions.
NaN is a special value in JavaScript that occurs when an invalid numeric calculation is performed. A notable characteristic of NaN is that it is the only value that is not equal to itself — NaN === NaN evaluates to false, so you cannot use equality operators to check for NaN.
A common practical pattern is to convert a user-provided value to a number and then use Number.isNaN() or Number.isFinite() to verify that the result is a valid number.
Browser Compatibility
24 or earlier ×
14 or earlier ×
14 or earlier ×
Android Browser
37+ ○
4 or earlier ×
Chrome Android
36+ ○
24 or earlier ×
Firefox Android
79+ ○
14 or earlier ×If you find any errors or copyright issues, please contact us.