NaN and undefined in Javascript

Great question! NaN (Not-a-Number) and undefined are different in JavaScript, and understanding their differences is crucial. Here's a breakdown:


🔹 Difference Between NaN and undefined

FeatureNaN (Not-a-Number)undefined
MeaningRepresents an invalid number operationRepresents a variable that has been declared but not assigned a value
Typenumberundefined
When does it occur?When a mathematical operation fails (e.g., parseFloat("hello"))When accessing a non-existent variable or property
ExampleparseFloat("abc") // NaNlet x; console.log(x); // undefined
Detect withisNaN(value) or Number.isNaN(value)typeof value === "undefined"

🔥 Key Cases You Should Remember

  1. When does NaN appear?

    • Trying to convert a non-numeric string into a number:

        parseFloat("hello"); // NaN
        Number("abc"); // NaN
      
    • Mathematical operations that don't make sense:

        Math.sqrt(-1); // NaN (in JavaScript, not complex numbers)
        0 / 0; // NaN
      
    • Performing operations with NaN results in NaN:

        let x = NaN;
        console.log(x + 5); // NaN
      
  2. When does undefined appear?

    • Accessing an undeclared variable:

        console.log(myVar); // ReferenceError: myVar is not defined
      
    • Accessing an uninitialized variable:

        let x;
        console.log(x); // undefined
      
    • Accessing an object property that doesn't exist:

        let obj = {};
        console.log(obj.prop); // undefined
      
    • Function without return:

        function doSomething() {}
        console.log(doSomething()); // undefined
      

Important Things to Remember

  • NaN is actually a number, but it means "invalid number".

  • undefined is its own type and usually means "missing value".

  • NaN is NOT equal to itself (NaN !== NaN), so you should check it using:

      Number.isNaN(value); // ✅ Best way to check for NaN
    
  • Avoid using isNaN(value) directly, because it tries to convert values to numbers:

      isNaN("hello"); // true (confusing)
      Number.isNaN("hello"); // false (correct)
    
  • Use typeof value === "undefined" to check for undefined:

      let a;
      console.log(typeof a === "undefined"); // true
    

TL;DR

  • Use Number.isNaN(value) to check for NaN.

  • Use typeof value === "undefined" to check for undefined.

  • NaN usually means an invalid number operation.

  • undefined usually means a missing or uninitialized value.

0
Subscribe to my newsletter

Read articles from Dikshant Koriwar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Dikshant Koriwar
Dikshant Koriwar

Hi, I'm Dikshant – a passionate developer with a knack for transforming ideas into robust, scalable applications. I thrive on crafting clean, efficient code and solving challenging problems with innovative solutions. Whether I'm diving into the latest frameworks, optimizing performance, or contributing to open source, I'm constantly pushing the boundaries of what's possible with technology. On Hashnode, I share my journey, insights, and the occasional coding hack—all fueled by curiosity and a love for continuous learning. When I'm not immersed in code, you'll likely find me exploring new tech trends, tinkering with side projects, or simply enjoying a great cup of coffee. Let's connect, collaborate, and build something amazing—one line of code at a time!