🎓 Free Capstone Projects with Full Documentation, ER Diagrams & Source Code — Updated Weekly for 2026
👨‍💻 Free Source Code & Capstone Projects for Developers

What does NaN mean in JavaScript? Explanations and Examples

Are you ready to know what does NAN means in JavaScript?

You can also check if a number is NaN or Infinity in JavaScript, so keep reading!

In this article, we will learn about the meaning of NaN (Not a Number) in JavaScript, including explanations and examples.

Discover how to check for NaN values, handle edge cases, and avoid errors in your code.

What is NaN in JavaScript?

NaN stands for “Not a Number,” and it’s a term used in JavaScript to describe a value that is not a valid number or not a legal number in JavaScript.

NaN is used to indicate that a value is not a number, and it can be returned from various operations that fail to produce a valid number.

For instance, if you are trying to parse a string that cannot be converted into a number, the result will be NaN.

In the same way, if you try to do mathematical operations using operands that aren’t numbers or can’t be turned into numbers, the output will be NaN (which means “not a number”).

Here’s an example:

let a = 0/0; // Division by zero
console.log(a); // NaN

let b = Math.sqrt(-1); // Square root of a negative number
console.log(b); // NaN

let c = "Itsourcecode" * 2; // Multiplying a string by a number
console.log(c); // NaN

Output:

NaN
NaN
NaN

One thing you should keep in mind is that NaN is not equal to any value, not even itself!

So if you want to check if something is NaN, you can’t just use the usual equality operators (== or ===).

Instead, use the isNaN() function or the Number.isNaN() function.

Exploring NAN Scenarios and Examples

NAN can emerge in various scenarios, and it’s essential to recognize them to prevent unexpected behavior in your JavaScript code.

Here are some common situations where NaN might come into play:

Mathematical Operations

NAN often appears when you perform arithmetic operations that involve non-numeric values.

For instance:

const sample = "Itsourcecode" / 10;
console.log(sample);

Output:

NaN

Missing or Invalid Input

When parsing strings to numbers, invalid input can lead to NAN.

const userInput = "itsourcecode123";
const numericValue = parseFloat(userInput);
console.log(numericValue); 

Output:

NaN

Undefined Variables

Using undefined variables in calculations can result in NAN.

let sample;
const calculation = 20 * sample;
console.log(calculation);

Output:

NaN

Functions That Return NAN

Certain JavaScript functions return NAN under specific circumstances.

const nanValue = parseInt("Itsourcecode");
console.log(nanValue);

Output:

NaN

Mathematical Expressions

Complex expressions involving NAN can yield unexpected results.

const sample = Math.sqrt(-1);
console.log(sample);

Output:

NaN

How to fix NaN error in JavaScript?

To fix a NaN error in JavaScript, you need to identify the source of the error and ensure that the operation that is producing the NaN value is valid.

Check your inputs

When working with numbers in JavaScript, it’s important to make sure that the values you’re using are actually numbers.

You can use the isNaN() or Number.isNaN() functions to check if a value is NaN, which stands for “Not a Number.” This can help you catch errors and avoid unexpected results.

Use type coercion

Another thing to keep in mind is that JavaScript will try to convert non-numeric values into numbers when performing arithmetic operations.

However, this can sometimes produce unexpected results.

To avoid this, you can use type coercion to explicitly convert values into numbers before performing calculations.

Handle edge cases

It’s important to handle edge cases when working with mathematical operations.

Some operations have edge cases that can produce NaN values, such as dividing by zero or taking the logarithm of a negative number.

Ensure to handle these edge cases in your code to avoid NaN errors.

How to check Nan in JavaScript?

You can check if a value is NaN (Not-A-Number) in JavaScript using the isNaN() or Number.isNaN() functions.

Use isNaN() function

The isNaN() function is a global function that returns true if the value passed to it is NaN, and false otherwise.

Here’s an example:

let a = 0/0; // a is NaN
console.log(isNaN(a)); 

Output:

true

Use Number.isNaN() function

The Number.isNaN() function is a more reliable way to check for NaN values, as it doesn’t try to convert the value passed to it into a number before checking if it’s NaN.

Here’s an example:

let  b = 0/0; // b is NaN
console.log(Number.isNaN(b)); 

Output:

true

As we mentioned earlier, NaN is not equal to any value, including itself.

It simply means you cannot use the equality operator (== or ===) to test whether a value is NaN.

Instead, you should use the two functions above the isNaN() or Number.isNaN().

How to Check for NaN (isNaN vs Number.isNaN)

The global isNaN() function and the newer Number.isNaN() behave differently. This trips up most JavaScript developers at least once:

// Global isNaN() — COERCES first, then checks
isNaN(NaN);          // true
isNaN("hello");      // true  (Coerces "hello" to NaN first, then checks)
isNaN("123");        // false (Coerces to 123, not NaN)
isNaN(undefined);    // true  (Coerces to NaN)

// Number.isNaN() — STRICT check, no coercion
Number.isNaN(NaN);          // true
Number.isNaN("hello");      // false (it's a string, not NaN)
Number.isNaN(undefined);    // false (it's undefined, not NaN)

Rule: always use Number.isNaN() for the strict check. The global isNaN() has historical quirks that produce surprising results. The only reason to use the global one is supporting Internet Explorer 11, which has no Number.isNaN.

The NaN === NaN Surprise

console.log(NaN === NaN);   // false (!!)
console.log(NaN == NaN);    // false (!!)
console.log(NaN !== NaN);   // true

// NaN is the ONLY value in JavaScript that is not equal to itself
const x = NaN;
console.log(x === x);   // false

This is by IEEE 754 design, not a JavaScript bug. NaN by definition is “not equal to anything”, including itself. This is why you cannot check for NaN with ===. Use Number.isNaN() or Object.is(value, NaN) instead.

Safe Number Parsing With NaN Awareness

function parsePrice(str) {
    const n = parseFloat(str);
    if (Number.isNaN(n)) return 0;
    return n;
}

console.log(parsePrice("123.45"));    // 123.45
console.log(parsePrice("free"));      // 0 (was NaN)
console.log(parsePrice(""));          // 0
console.log(parsePrice("9.99 USD"));  // 9.99 (parseFloat stops at "U")

Without the NaN check, your 9.99 USD price string would happily parse to 9.99, but "free" would silently turn into NaN and break every subsequent calculation. Always check the result of parseFloat/parseInt before using it.

Common NaN Mistakes

  • Checking for NaN with ===. Always returns false. Use Number.isNaN(value).
  • Using global isNaN() instead of Number.isNaN(). The global version coerces the argument first, leading to surprises like isNaN("hello") returning true.
  • Forgetting that math with NaN produces NaN. NaN + 5 is NaN. NaN * 0 is NaN. Any operation involving NaN cascades, so one bad input poisons the whole calculation.
  • Not checking parseInt / parseFloat results. parseInt("abc") returns NaN silently. Always validate before using the parsed value.
  • Trying to JSON-serialize NaN. JSON.stringify(NaN) returns “null”, not “NaN”. This loses information silently. Convert to a sentinel value (like null) explicitly before serializing.

Conclusion

In conclusion, NaN stands for “Not a Number” and is a special value in JavaScript that represents something that isn’t a number.

It is usually the result of an operation that cannot produce a valid number, such as dividing zero by zero or taking the square root of a negative number.

To check if a value is NaN, you can use the isNaN() or Number.isNaN() functions. It is important to handle NaN values properly in your code to avoid errors and unexpected results.

We are hoping that this article provides you with enough information that helps you understand what does nan mean in JavaScript.

Frequently Asked Questions

What does NaN mean in JavaScript?

NaN stands for “Not a Number”. It is a special numeric value that represents an invalid or undefined result of a mathematical operation. Despite the name, NaN is a number type: typeof NaN returns “number”. Common ways to produce NaN: dividing 0/0, multiplying by undefined, parseFloat on a non-numeric string, or Math.sqrt(-1).

Why does NaN === NaN return false?

Because IEEE 754 (the floating-point standard JavaScript uses) explicitly defines NaN as “not equal to anything, including itself”. This is by design, not a bug. The reason: NaN represents an undefined or unrepresentable value, and two undefined results should never be considered equal. To check for NaN, use Number.isNaN(value) or Object.is(value, NaN).

What’s the difference between isNaN() and Number.isNaN()?

The global isNaN() COERCES the argument to a number first, then checks. So isNaN("hello") returns true because “hello” coerces to NaN. Number.isNaN() does NOT coerce — it only returns true for the actual NaN value. Always prefer Number.isNaN() for strict, predictable checks.

How do I check if a value is NaN in JavaScript?

Use Number.isNaN(value) (ES6+). For older browsers, use value !== value — the only value not equal to itself is NaN. Do NOT use value === NaN (always returns false) or the global isNaN() (coerces and gives surprising results).

What causes NaN in real-world JavaScript code?

Top causes: (1) parseFloat or parseInt on a non-numeric string like parseInt("free"); (2) math involving undefined like 5 * undefined; (3) accessing a missing object property in math like user.age + 1 when age is undefined; (4) Math functions on invalid inputs like Math.sqrt(-1) or Math.log(-5). Defensive code always validates inputs before math.

Related JavaScript Tutorials

Thank you for reading itsourcecoders 😊.

Leave a Comment