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
Feature | NaN (Not-a-Number) | undefined |
Meaning | Represents an invalid number operation | Represents a variable that has been declared but not assigned a value |
Type | number | undefined |
When does it occur? | When a mathematical operation fails (e.g., parseFloat("hello") ) | When accessing a non-existent variable or property |
Example | parseFloat("abc") // NaN | let x; console.log(x); // undefined |
Detect with | isNaN(value) or Number.isNaN(value) | typeof value === "undefined" |
🔥 Key Cases You Should Remember
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 inNaN
:let x = NaN; console.log(x + 5); // NaN
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 anumber
, 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 forNaN
.Use
typeof value === "undefined"
to check forundefined
.NaN
usually means an invalid number operation.undefined
usually means a missing or uninitialized value.
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!