Sajiron
null, undefined, and NaN in JavaScript
JavaScript often surprises developers with its type system. Three values that frequently trip up beginners and even seasoned developers are: null, undefined, and NaN.
All three indicate “something went wrong” or “a value is missing”, but they do so in different contexts — and mishandling them can result in hard-to-debug issues.
In this article, you’ll learn:
What each value actually represents
Where you’re most likely to encounter them
How to test for them correctly
And practical patterns for avoiding unexpected bugs
Let’s break them down, one by one.
undefined?undefined in JavaScript is a primitive value that signals the absence of a defined value. It is automatically assigned by the JavaScript engine in several situations.
undefined?You’ll encounter undefined in these common cases:
a) Declared but not assigned:
let count;
console.log(count); // undefinedb) Accessing non-existent properties:
const user = { name: "Sara" };
console.log(user.age); // undefinedc) Missing function parameters:
function greet(name) {
console.log(`Hello, ${name}`);
}
greet(); // Hello, undefinedd) Implicit returns:
function doNothing() {}
console.log(doNothing()); // undefinedJavaScript functions return undefined by default unless told otherwise.
undefined is often used by the JS runtime — you typically shouldn’t assign it yourself.
It is falsy, meaning it behaves like false in conditional logic.
typeof undefined; // "undefined"This is the only JavaScript value that returns "undefined" from typeof.
null?null is also a primitive type, but it represents an intentional absence of any value.
Whereas undefined is often accidental or implicit, null is explicit. You, as the developer, set it.
nulla) Resetting a variable:
let data = fetchData();
data = null; // clear outb) Representing empty values:
const user = {
name: "Sara",
profile: null, // no profile yet
};c) Used with optional chaining:
if (user.profile?.bio === null) {
console.log("No bio yet.");
}It’s great for explicitly saying "this has no value yet."
In many APIs and databases, null is used as a placeholder for "not available."
typeof null; // "object"This is a well-known bug in JavaScript that’s been around since the beginning. Don’t be fooled — null is not an object.
NaN?NaN stands for “Not-a-Number”, but paradoxically, it is actually of type number.
It is a special value that arises only from numeric operations that fail to produce a valid number.
NaNa) Invalid math operations:
const result = 0 / 0; // NaNb) Coercion failures:
parseInt("abc"); // NaN
Math.sqrt(-1); // NaNc) Operation on undefined:
let x;
console.log(x * 10); // NaNNaN is falsy but not equal to itself.
You can’t use === to check for it directly.
NaNconsole.log(NaN === NaN); // false ❌
console.log(Number.isNaN(NaN)); // true ✅Avoid using isNaN() globally — it coerces its argument. Prefer Number.isNaN() for accuracy.
Here’s a breakdown:
Feature |
|
|
|
Meaning | Not yet assigned | Assigned intentionally as empty | Invalid number |
Commonly caused by | Runtime (JS engine) | Developer choice | Bad math or coercion |
Type |
|
|
|
Equality ( |
|
|
|
Strict equality ( |
|
|
|
Falsy? | ✅ Yes | ✅ Yes | ✅ Yes |
undefined as nullconst data = {};
if (data.value === null) {
console.log("It's null!");
}
// Won’t log if it's actually `undefined`Use == null if you want to catch both:
if (data.value == null) {
console.log("It's null or undefined!");
}NaN in calculations silently breaks thingslet total = 100 + parseFloat("abc"); // NaNUse Number.isNaN() to guard against this.
undefinedobj.value = undefined; // Better: delete obj.value; or use null✅ Use null when resetting values or signaling empty placeholders.
❌ Avoid assigning undefined directly — let JavaScript do it.
✅ Always use Number.isNaN() over == NaN or global isNaN().
🧪 Use optional chaining (?.) and nullish coalescing (??) for safe access.
const name = user.profile?.name ?? "Guest";JavaScript offers a lot of flexibility, but with that comes quirks.
To recap:
undefined means something is missing by default.
null means something is missing on purpose.
NaN means something is mathematically wrong.
Mastering these subtle differences will help you write defensive, bug-free code and debug tricky issues with confidence.

Learn the difference between debouncing and throttling in JavaScript. Improve performance and user experience with simple examples and use cases.

Explore essential math for machine learning—vectors, matrices, calculus, and probability—explained with beginner-friendly JavaScript code.

Learn how to structure Rust projects with packages, crates, and modules for better maintainability, reusability, and scalability.

Learn data preprocessing in JavaScript for Machine Learning. Clean, transform, and structure data with practical examples to boost ML model accuracy!