C#  

What is the Use of the isNaN Function?

πŸš€ Introduction

When working with numbers in JavaScript, you may encounter values that are not valid numbers. These invalid values are represented as NaN, which stands for Not-a-Number. To check whether a value is NaN, JavaScript provides the isNaN() function.

This function helps you write safer programs by making sure you do not perform calculations with invalid numbers.

πŸ“š What is NaN?

NaN means Not-a-Number. It is an exceptional value in JavaScript used to show that a calculation or conversion failed.

Example

console.log(0 / 0);           // NaN
console.log(Math.sqrt(-1));   // NaN
console.log(parseInt("Hello")); // NaN
  • 0 / 0 does not make sense in math.

  • The square root of a negative number is not real.

  • Converting the word "Hello" into a number is not possible.

All these cases give NaN.

πŸ”Ž What is isNaN()?

The isNaN() function checks if a value is NaN or not. If the value is NaN, it returns true. Otherwise, it returns false.

Syntax

isNaN(value)

Example

console.log(isNaN(123));      // false (123 is a number)
console.log(isNaN("Hello")); // true (cannot convert to a number)
console.log(isNaN(NaN));      // true (it is NaN)

🧩 How isNaN() Works

The isNaN() function first tries to convert the given value into a number. After that:

  • If conversion works, it returns false (because the value is a valid number).

  • If conversion fails, it returns true (because the value is NaN).

Example

console.log(isNaN("100")); // false ("100" becomes 100)
console.log(isNaN("abc")); // true (cannot convert to number)

So, isNaN("100") is false because "100" can be converted into the number 100. But isNaN("abc") is true because "abc" is not a number.

⚠️ Limitations of isNaN()

One issue with isNaN() is that it sometimes gives confusing results because it converts the value before checking.

Example

console.log(isNaN(true));      // false (true becomes 1)
console.log(isNaN(false));     // false (false becomes 0)
console.log(isNaN(undefined)); // true (cannot convert to number)

Here, true and false are not numbers, but after conversion, they become 1 and 0, so isNaN() says they are numbers.

πŸ› οΈ Number.isNaN() vs isNaN()

To fix the confusion, JavaScript introduced Number.isNaN() in ES6. This function checks only if the value is exactly NaN, without trying to convert it.

Example

console.log(isNaN("Hello"));        // true (after conversion)
console.log(Number.isNaN("Hello")); // false (string is not NaN)

console.log(isNaN(NaN));        // true
console.log(Number.isNaN(NaN)); // true
  • isNaN("Hello") is true because "Hello" cannot be converted.

  • Number.isNaN("Hello") is false because "Hello" is not exactly NaN.

This makes Number.isNaN() more reliable.

βœ… When to Use isNaN()

You should use isNaN() or Number.isNaN() when:

  • You want to check if a user’s input is valid or not.

  • You are doing calculations and need to make sure the values are real numbers.

  • You want to prevent errors when invalid data appears in your program.

Example

let userInput = "Hello";
if (isNaN(userInput)) {
  console.log("Please enter a valid number.");
} else {
  console.log("Your number is: " + userInput);
}

πŸ“ Summary

The isNaN() function in JavaScript is used to check if a value is Not-a-Number (NaN). It works by converting values into numbers before checking, which sometimes gives unexpected results. To avoid confusion, you can use Number.isNaN(), which only returns true if the value is exactly NaN. Both of these functions are very useful when you need to validate inputs and make sure calculations do not break due to invalid numbers.