null and undefined
JavaScript has two primitive values that represent absence:
undefinednull
At first, they can seem like the same thing because both mean "no value" in some way. But they have different meanings and different use cases.
The short version is:
undefinedusually means JavaScript has not been given a value yet.nullusually means the developer intentionally set the value to empty.
undefined: The Default Absence
undefined means a value has not been assigned.
It is JavaScript's way of saying, "This exists, but it does not have a value yet."
Declared but Not Assigned
If you declare a variable without assigning a value, JavaScript gives it undefined.
let message;
console.log(message); // undefinedThe variable exists, but it does not contain a meaningful value yet.
Missing Function Arguments
If a function expects an argument but you do not pass one, the parameter becomes undefined.
function greet(name) {
console.log(name);
}
greet(); // undefinedThe parameter name exists inside the function, but no value was passed into it.
Functions Without return
If a function does not return a value, JavaScript returns undefined automatically.
function logMessage() {
console.log("Hello");
}
const result = logMessage();
console.log(result); // undefinedThe function prints "Hello", but it does not return a value.
Missing Object Properties
If you try to access a property that does not exist, JavaScript returns undefined.
const user = {
name: "Asha"
};
console.log(user.age); // undefinedThe object exists. The age property does not.
null: Intentional Absence
null means an intentional empty value.
It is a value you assign when you want to clearly say, "There is no value here on purpose."
let currentUser = "Asha";
console.log(currentUser); // Asha
currentUser = null;
console.log(currentUser); // nullIn this example, null is useful because the user was once present, but now there is intentionally no current user.
Common situations where null makes sense:
- No selected user
- No active modal
- No search result
- No current DOM element reference
- A value that will be filled later
Example:
let selectedProduct = null;
// Later, after the user clicks a product
selectedProduct = "Laptop";typeof null
typeof behaves as expected with undefined:
console.log(typeof undefined); // undefinedBut typeof null has a famous result:
console.log(typeof null); // objectThis is a historical bug in JavaScript.
null is not actually an object. It is a primitive value. The bug was kept for backward compatibility because changing it would break old websites.
So remember:
typeof null; // "object", but null is still a primitiveComparing null and undefined
null and undefined are loosely equal:
console.log(null == undefined); // trueBut they are not strictly equal:
console.log(null === undefined); // falseWhy?
- Loose equality
==allows type conversion. - Strict equality
===checks both value and type.
In modern JavaScript, prefer strict equality most of the time:
value === null;
value === undefined;However, there is one common exception.
Checking for Either null or undefined
Sometimes you want to check whether a value is either null or undefined.
In that specific case, this pattern is common:
if (value == null) {
console.log("Value is null or undefined");
}This works because:
null == undefined; // trueBut it does not match other falsy values:
console.log(0 == null); // false
console.log("" == null); // false
console.log(false == null); // falseSo value == null is a concise way to check for only null or undefined.
Use it intentionally and make sure your team understands the pattern.
Falsy Behavior
Both null and undefined are falsy.
console.log(Boolean(null)); // false
console.log(Boolean(undefined)); // falseThat means they behave like false in conditions.
let currentUser = null;
if (currentUser) {
console.log("User exists");
} else {
console.log("No current user");
}This can be useful, but be careful. Falsy checks also match values like 0, "", false, and NaN.
If you specifically need to check for absence, use a direct check:
if (currentUser === null) {
console.log("No current user");
}Or:
if (currentUser == null) {
console.log("No current user or value was not loaded");
}Best Practices
Let JavaScript Use undefined
Avoid manually assigning undefined in most cases.
Less useful:
let data = undefined;Prefer:
let data;undefined usually means a value has not been initialized yet.
Use null for Intentional Empty Values
Use null when you want to clearly reset or empty something.
let activeElement = document.querySelector("button");
// Later
activeElement = null;This communicates that the variable intentionally no longer points to an element.
Use Strict Checks When Meaning Matters
If the difference matters, check directly:
if (value === undefined) {
console.log("Value was not assigned");
}
if (value === null) {
console.log("Value was intentionally cleared");
}Comparison Table
| Feature | undefined |
null |
|---|---|---|
| Meaning | Value has not been assigned | Value is intentionally empty |
| Usually created by | JavaScript | Developer |
Type from typeof |
"undefined" |
"object" |
| Primitive? | Yes | Yes |
| Falsy? | Yes | Yes |
| Strictly equal to each other? | No | No |
| Loosely equal to each other? | Yes | Yes |
Practical Example
Imagine a search feature.
let searchResult;
console.log(searchResult); // undefinedAt this point, the search has not happened yet.
After the search runs, maybe no result is found:
searchResult = null;
console.log(searchResult); // nullNow the meaning is different:
undefined: Search has not produced a value yet.null: Search ran, but there is intentionally no result.
That distinction makes your code easier to reason about.
Summary
undefined and null both represent absence, but they are not the same.
Remember:
undefinedusually means a value has not been assigned.nullmeans the developer intentionally set an empty value.typeof nullreturns"object"because of a historical JavaScript bug.null == undefinedistrue, butnull === undefinedisfalse.- Use
nullwhen you want to clearly represent intentional emptiness. - Avoid manually assigning
undefinedunless you have a specific reason.
Next, you will learn about symbols and bigints, two less common but important primitive types.