NaN
(Not-a-Number) is a special numeric value in JavaScript that represents the result of an invalid or undefined mathematical operation. Despite its name, its data type is number
. The most reliable way to check for NaN
is the Number.isNaN()
method, because NaN
is not equal to anything, including itself (NaN === NaN
returns false
).
NaN
is a global property representing the value “not a number”. It is one of the special values of the number
type in the IEEE 754 standard, used to represent results that cannot be expressed as real numbers.
Despite its name, NaN
is technically a number.
console.log(typeof NaN); // "number"
Features:
NaN
belongs to the number
type.NaN
is returned when a mathematical operation cannot produce a meaningful numeric value.
// Dividing zero by zero
console.log(0 / 0); // NaN
// Taking the square root of a negative number
console.log(Math.sqrt(-1)); // NaN
// Multiplying infinity by zero
console.log(Infinity * 0); // NaN
When JavaScript tries to convert a string that does not contain a number into a number, the result will be NaN
.
// Explicit conversion
console.log(parseInt('hello')); // NaN
console.log(Number('abc')); // NaN
// Implicit conversion in mathematical operations
console.log('string' - 10); // NaN
console.log('10a' * 5); // NaN
Features:
NaN
is “contagious”: any operation with NaN
will return NaN
.console.log(1 + NaN); // NaN
console.log('text' + NaN); // "textNaN" (string concatenation occurs here)
Checking for NaN
has its peculiarities, as NaN
is the only value in JavaScript that is not equal to itself.
console.log(NaN === NaN); // false
console.log(NaN == NaN); // false
Number.isNaN()
(recommended method)This is the most reliable and preferred method. It checks if the passed value is NaN
and does not try to convert it.
console.log(Number.isNaN(NaN)); // true
console.log(Number.isNaN(123)); // false
console.log(Number.isNaN('abc')); // false (does not convert the string to a number)
console.log(Number.isNaN(undefined)); // false
Features:
NaN
.isNaN()
This method is less reliable because it first tries to convert the argument to a number.
console.log(isNaN(NaN)); // true
console.log(isNaN('abc')); // true, because Number('abc') -> NaN
console.log(isNaN(undefined)); // true, because Number(undefined) -> NaN
console.log(isNaN({})); // true, because Number({}) -> NaN
Features:
This is a “hacky” but working method based on the unique property of NaN
.
function isValueNaN(value) {
return value !== value;
}
console.log(isValueNaN(NaN)); // true
console.log(isValueNaN(123)); // false
console.log(isValueNaN('abc')); // false
Features:
Number.isNaN()
.// What will this code output to the console?
console.log(typeof NaN);
console.log(NaN === NaN);
console.log(Number.isNaN('hello'));
console.log(isNaN('hello'));
// Write a function that takes an array and returns
// only numeric values, excluding NaN.
function filterNumbers(arr) {
// Your code
}
const data = [1, 'a', 2, NaN, 3, undefined, 4, null, '5'];
console.log(filterNumbers(data)); // [1, 2, 3, 4]
function filterNumbers(arr) {
return arr.filter(item => typeof item === 'number' && !Number.isNaN(item));
}
Explanation: We filter the array, keeping only elements with the type number
, and additionally ensure that it is not NaN
using Number.isNaN()
.