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.

5 min read

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.

undefinednull
MeaningNo value was ever assignedIntentionally empty
Who sets itJavaScript engine (automatic)You (explicit assignment)
typeof"undefined""object" (known bug)
Default forUnassigned variables, missing arguments, missing propertiesNothing; must be assigned
Loose equalitynull == undefined is trueundefined == null is true
Strict equalitynull === undefined is falseundefined === 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:

javascriptjavascript
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:

javascriptjavascript
function doNothing() {}
let result = doNothing();
console.log(result); // undefined

Accessing an object property that does not exist returns undefined instead of throwing an error. This is a key difference from many other languages:

javascriptjavascript
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:

javascriptjavascript
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.

javascriptjavascript
let selectedUser = null; // no user selected right now
let searchResults = null; // search has not run yet

null 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:

javascriptjavascript
let element = document.querySelector(".nonexistent");
console.log(element); // null

How 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.

javascriptjavascript
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:

javascriptjavascript
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:

javascriptjavascript
let data = undefined; // Bad: did the developer forget or intend this?
let data = null;      // Good: clearly intentional

Always 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

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.
RunePowered by Rune AI

Frequently Asked Questions

Is null == undefined true or false?

null == undefined is true with loose equality because both represent emptiness. null === undefined is false with strict equality because they are different types. Always use strict equality (===) to distinguish them.

When should I use null instead of undefined?

Use null when you want to intentionally mark a value as empty or not available. Let undefined be the default that JavaScript assigns when something has no value. Never manually assign undefined to a variable.

What does typeof return for null and undefined?

typeof undefined returns 'undefined'. typeof null returns 'object', which is a known bug in JavaScript. To check for null reliably, use value === null instead of typeof.

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.