Last modified: Jul 17, 2026

JavaScript Undefined vs Null Variables

In JavaScript, undefined and null are two special values that represent "nothing" or "empty".

They look similar but behave very differently. Beginners often confuse them.

This guide explains the difference between undefined and null in simple terms.

You will learn when and why each value appears. We also cover how to check for them.

Let's start with the basics.

What is Undefined?

Undefined is a JavaScript primitive type. It means a variable has been declared but not assigned a value.

JavaScript automatically sets a variable to undefined when you create it without a value.

Here is a simple example:

 
// Variable declared but no value
let myName;
console.log(myName); // Output: undefined

undefined

Functions also return undefined when no explicit return is given.

 
function doNothing() {
  // no return statement
}
console.log(doNothing()); // Output: undefined

undefined

Accessing an object property that does not exist also gives undefined.

 
const person = { name: "Alice" };
console.log(person.age); // Output: undefined

undefined

What is Null?

Null is also a primitive type. It represents an intentional absence of any object value.

Unlike undefined, null is not set automatically. You assign it yourself.

Developers use null to indicate that a variable should be empty on purpose.

 
// Developer explicitly sets null
let myCar = null;
console.log(myCar); // Output: null

null

Null is often used for object references that are intentionally empty.

For example, when a function expects an object but none is available.

Key Differences Between Undefined and Null

Here are the main differences you need to remember.

1. Type: typeof undefined is "undefined". typeof null is "object".

 
console.log(typeof undefined); // "undefined"
console.log(typeof null);      // "object"

undefined
object

2. Automatic vs Manual: undefined appears automatically. null is assigned manually.

3. Equality: They are equal with == but not with ===.

 
console.log(null == undefined);  // true
console.log(null === undefined); // false

true
false

4. Default value: undefined is the default for uninitialized variables. null is not.

When to Use Undefined vs Null

Use undefined for uninitialized variables. Let JavaScript handle it.

Use null when you want to explicitly clear a variable or object.

For example, resetting a user's session:

 
let currentUser = { name: "Bob" };
// Later, user logs out
currentUser = null; // Intentional empty

Do not assign undefined manually. That is a common mistake.

Let the language handle undefined. You control null.

Checking for Undefined and Null

You can check for both values using strict equality (===).

 
let value1;
let value2 = null;

if (value1 === undefined) {
  console.log("value1 is undefined");
}

if (value2 === null) {
  console.log("value2 is null");
}

value1 is undefined
value2 is null

A safer way is to check if a variable has any value at all:

 
if (value1 == null) {
  console.log("value1 is either null or undefined");
}

value1 is either null or undefined

This works because null == undefined is true.

Common Pitfalls

One common mistake is using typeof to check for null. It returns "object".

Another is accidentally setting a variable to undefined.

Avoid writing code like let x = undefined;. Use null instead.

Also, remember that undefined is a global property. It can be overwritten in older JavaScript, but that is rare now.

Examples in Real Code

Here is a function that returns null when no result is found:

 
function findUser(id) {
  const users = [{ id: 1, name: "Alice" }];
  const user = users.find(u => u.id === id);
  return user || null; // Return null if not found
}

console.log(findUser(1)); // { id: 1, name: "Alice" }
console.log(findUser(2)); // null

{ id: 1, name: 'Alice' }
null

And here is a function that returns undefined by default:

 
function greet() {
  // No return
}
console.log(greet()); // undefined

undefined

Relation to Variable Types

Understanding undefined and null helps with variable typing.

When you declare a variable without a value, its type is undefined.

If you want to learn more about variable types, check our JavaScript Variable Types Guide.

Also, proper variable naming helps avoid confusion. See our JavaScript Variable Naming Rules.

For more examples, visit our JavaScript Variables Examples page.

Conclusion

Undefined and null are both empty values but serve different purposes.

Undefined means a variable exists but has no value assigned. It happens automatically.

Null means an intentional empty value. You set it yourself.

Always use === to check for each one separately.

Use null to clear variables. Let JavaScript handle undefined.

Now you understand the difference. Practice using both in your code.

This will make your JavaScript cleaner and more predictable.