Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Why is isNaN(null) == false in JS?
The isNaN() function in JavaScript is used to check whether a given value is NaN. This function converts the argument to a number first, then checks if it's NaN. When you pass null to isNaN(), it returns false, which can be surprising. This happens because of JavaScript type coercion when using isNaN(). In this article, we will understand why isNaN returns false for null.
Understanding isNaN and Type Coercion
Before moving forward, you need to understand these key concepts:
- isNaN() Function: The isNaN() function is used to check whether a given value is NaN. This function converts the argument to a number first, then checks if it's NaN.
- Type Coercion: Type coercion in JavaScript is the process of converting values automatically or implicitly from one data type to another.
How isNaN() Works
The global isNaN() function follows a two-step process:
- It first converts the argument into a number using the Number() function.
- Then it checks if the resulting value is NaN and returns a boolean value.
Why Does isNaN(null) Return false?
When you pass null to isNaN(), JavaScript performs type coercion and converts it to a number before checking if it's NaN:
- null is converted to a number using Number(null), which results in 0.
- Since 0 is a valid number and not NaN, isNaN(0) returns false.
Example: null Conversion
Let's see how null is converted and checked:
// First, null is converted to a number console.log(Number(null)); // 0 // Then isNaN checks if 0 is NaN console.log(isNaN(null)); // false // This is equivalent to: console.log(isNaN(0)); // false
0 false false
Comparison with Other Values
Let's compare how different values behave with isNaN():
console.log("Value\t\tNumber()\tisNaN()");
console.log("null\t\t" + Number(null) + "\t\t" + isNaN(null));
console.log("undefined\t" + Number(undefined) + "\t\t" + isNaN(undefined));
console.log("''\t\t" + Number('') + "\t\t" + isNaN(''));
console.log("'hello'\t\t" + Number('hello') + "\t\t" + isNaN('hello'));
Value Number() isNaN() null 0 false undefined NaN true '' 0 false 'hello' NaN true
Using Number.isNaN() for Strict Checking
Number.isNaN() doesn't perform type conversion and strictly checks if a value is exactly NaN:
console.log("Global isNaN vs Number.isNaN:");
console.log("isNaN(null):\t\t" + isNaN(null));
console.log("Number.isNaN(null):\t" + Number.isNaN(null));
console.log("Number.isNaN(NaN):\t" + Number.isNaN(NaN));
Global isNaN vs Number.isNaN: isNaN(null): false Number.isNaN(null): false Number.isNaN(NaN): true
Comparison Table
| Value | Number() Result | isNaN() | Number.isNaN() |
|---|---|---|---|
| null | 0 | false | false |
| undefined | NaN | true | false |
| NaN | NaN | true | true |
| "" | 0 | false | false |
Conclusion
isNaN(null) returns false because null is converted to 0 during type coercion, and 0 is not NaN. Understanding this type coercion behavior is crucial when working with JavaScript's number checking functions. Use Number.isNaN() when you need strict NaN checking without type conversion.
