This post will discuss how to check for NaN values in JavaScript.

Checking for NaN values in JavaScript can be tricky, because NaN is a special value that represents the result of an invalid or undefined mathematical operation. NaN stands for Not a Number, but its type is actually number. Therefore, we cannot use the usual equality operators (== or ===) to compare a value with NaN, because NaN is not equal to any other value, including itself. For instance:

console.log(NaN == NaN); // false
console.log(NaN === NaN); // false
console.log(NaN == 42); // false
console.log(NaN === 42); // false

There are several ways to check for NaN values in JavaScript, but some of them are more reliable than others. Here are some of the methods we can use:

1. Using isNaN() function

One way to check for NaN is to use the built-in function isNaN(), which returns true if the argument is NaN, and false otherwise. However, this function can also return true for some non-numeric values that are converted to NaN internally, such as strings or undefined. For instance:

isNaN(0/0); // true
isNaN(NaN); // true
isNaN(3.14); // false
isNaN(‘Hello’); // true
isNaN(undefined); // true

2. Using Number.isNaN() function

Another way to check for NaN is to use the Number.isNaN() function, which was introduced in ECMAScript 6. This function returns true only if the argument is NaN, and false for any other value. Unlike the global isNaN() function, this function does not coerce the value to a number, so it only returns true for actual NaN values. Here’s an example:

Number.isNaN(0/0); // true
Number.isNaN(NaN); // true
Number.isNaN(3.14); // false
Number.isNaN(‘Hello’); // false
Number.isNaN(undefined); // false

This function is more reliable than isNaN(), as it does not perform any type conversion before testing the value.

3. Using Object.is() function

The Object.is() function is a static function of the Object object that returns true if the given values are the same value, and false otherwise. This function can also check for NaN values, because it considers NaN to be the same as itself, unlike the equality operators. For example, Object.is(x, NaN) returns true if x and NaN are the same value, and false otherwise.

Object.is(0/0, NaN); // true
Object.is(NaN, NaN); // true
Object.is(3.14, NaN); // false
Object.is(‘Hello’, NaN); // false
Object.is(undefined, NaN); // false

This is also a reliable way to check for NaN values, as it does not perform any type conversion or use the abstract equality algorithm.

4. Using a self-comparison

A third way to check for NaN is to use the self-inequality property of NaN. This is a trick that exploits the fact that NaN is the only value in JavaScript that is not equal to itself. Therefore, we can check for NaN values by comparing a value with itself using the !== operator. If the result is true, then the value is NaN. Here’s an example:

0/0 !== 0/0; // true
NaN !== NaN; // true
3.14 !== 3.14; // false
‘Hello’ !== ‘Hello’; // false
undefined !== undefined; // false

This function does not require any built-in function or function, but it may be less readable or intuitive than the other functions.

As we can see, there are different ways to check for NaN values in JavaScript, but some of them have drawbacks or limitations. The best way to avoid NaN values is to validate our inputs and handle errors properly in our code. However, if we need to check for NaN values explicitly, we should use the Number.isNaN() or Object.is() functions, as they are the most accurate and consistent functions.