undefined vs null in JavaScript: Key Differences Explained
undefined means a value was never assigned. null means a value is intentionally empty. Learn the key differences, when each appears, and how to check for both.
Comparing undefined vs null in JavaScript comes down to intent: both represent the absence of a value, but they mean different things. undefined is JavaScript's automatic default. null is a deliberate choice you make as a developer.
Understanding when each one appears and how to handle them correctly prevents subtle bugs in your code.
| undefined | null | |
|---|---|---|
| Meaning | No value was ever assigned | Intentionally empty |
| Who sets it | JavaScript engine (automatic) | You (explicit assignment) |
| typeof | "undefined" | "object" (known bug) |
| Default for | Unassigned variables, missing arguments, missing properties | Nothing; must be assigned |
| Loose equality | null == undefined is true | undefined == null is true |
| Strict equality | null === undefined is false | undefined === null is false |
Where undefined Appears
undefined shows up automatically in several situations. You never need to write it yourself. JavaScript uses it as the default when something has no value.
A variable declared without an assignment starts as undefined. This is the most common way beginners encounter it:
let name;
console.log(name); // undefined
console.log(typeof name); // "undefined"A function that does not return a value explicitly returns undefined. Every function without a return statement produces undefined as its result:
function doNothing() {}
let result = doNothing();
console.log(result); // undefinedAccessing an object property that does not exist returns undefined instead of throwing an error. This is a key difference from many other languages:
let user = { name: "Alex" };
console.log(user.age); // undefined (property does not exist)A function parameter that was not provided by the caller is undefined inside the function body. This lets you check whether an argument was passed:
function greet(name) {
console.log("Hello, " + name);
}
greet(); // "Hello, undefined"Where null Appears
null never appears on its own. It only exists because you explicitly assigned it. You use null to say this value is intentionally empty or not available yet.
let selectedUser = null; // no user selected right now
let searchResults = null; // search has not run yetnull is commonly used as an initial value for variables that will later hold an object. It signals that the variable exists but does not yet point to a meaningful object. This is different from undefined, which would mean the variable was never set up at all.
null is also a common return value from DOM methods when no element matches the selector. If you call document.querySelector with a selector that matches nothing, you get null, not undefined:
let element = document.querySelector(".nonexistent");
console.log(element); // nullHow to Check for Each
Because typeof null returns "object" due to a historical bug, you must check for null and undefined differently. For undefined, typeof is the safest check because it works even if the variable was never declared. For null, use strict equality.
let value = undefined;
// Check for undefined
console.log(value === undefined); // true
console.log(typeof value === "undefined"); // true
value = null;
// Check for null
console.log(value === null); // true
console.log(typeof value); // "object" (do not rely on this)If you need to check whether a value is either null or undefined in a single condition, use loose equality with null. The expression value == null is true for both null and undefined because they are loosely equal to each other. This is the one case where loose equality is actually useful:
function printValue(value) {
if (value == null) {
console.log("No value provided");
return;
}
console.log(value);
}
printValue(undefined); // "No value provided"
printValue(null); // "No value provided"
printValue("hello"); // "hello"Common Mistake: Assigning undefined
Some developers manually set variables to undefined to clear them. This is a bad practice. It makes it impossible to tell whether a value was never set or was intentionally cleared:
let data = undefined; // Bad: did the developer forget or intend this?
let data = null; // Good: clearly intentionalAlways use null when you want to mark something as empty. Let undefined be the signal that something was never assigned. This convention makes your code's intent clear to anyone reading it.
For a deeper look at why manually assigning undefined causes problems, see the guide on why you should never assign undefined. For the complete list of JavaScript data types where these values belong, read the data types guide.
Rune AI
Key Insights
- undefined is the default absence of value; null is intentional emptiness.
- undefined appears for unassigned variables, missing arguments, and missing properties.
- null must be explicitly assigned; it never appears automatically.
- Use === to check for null and typeof to check for undefined.
- Never assign undefined to a variable; use null instead.
Frequently Asked Questions
Is null == undefined true or false?
When should I use null instead of undefined?
What does typeof return for null and undefined?
Conclusion
undefined is JavaScript's default for anything that has no value: unassigned variables, missing function arguments, and missing object properties. null is an intentional marker you set to say this is empty on purpose. Always use strict equality to tell them apart, and never manually assign undefined.
More in this topic
Is JavaScript Frontend or Backend? Full Guide
JavaScript is both a frontend and backend language. Learn the difference between browser JavaScript and Node.js, what each is used for, and which one you should learn first.
Learn JavaScript Step by Step Tutorial with Real Examples
Follow a hands-on tutorial that teaches JavaScript by building a real interactive page. Write your first variables, functions, and event listeners with examples you can run.
JavaScript Tutorial: Complete Beginner's Guide to Programming in 2025
A structured learning path for absolute beginners. Learn what to study first, how to practice, and which JavaScript concepts matter most when you are starting from zero.