Null vs Undefined in JavaScript: Key Differences Explained with Examples

Null vs Undefined in JavaScript

Null vs Undefined in JavaScript: Key Differences Explained with Examples

When learning JavaScript, one of the most confusing topics for beginners (and even experienced developers) is the difference between null and undefined. Both represent “nothingness,” yet they behave differently in code, data types, and comparisons.

In this blog, we’ll break down the difference between null and undefined in JavaScript with examples, a comparison table, and also look at a few common interview questions related to this topic.

What is Undefined in JavaScript?

In JavaScript, variables are undefined when no value has been assigned to them. Unlike many programming languages where variables need explicit initialization, JavaScript assigns undefined by default.

Example:

let x;
console.log(x);           // Output: undefined
console.log(typeof x);    // Output: undefined

Here, x has been declared but not assigned any value. Therefore:

  • The value of x is undefined.
  • The type of x is also undefined.

If we assign a value later:

x = 10;
console.log(x);           // Output: 10
console.log(typeof x);    // Output: number

πŸ‘‰ Key Point:
If a variable has been declared but not initialized, it will always hold the value undefined.

What is Null in JavaScript?

null is an explicit assignment to indicate that a variable is empty or has no value. Unlike undefined, which JavaScript assigns automatically, null is set intentionally by the developer.

Example:

let y = null;
console.log(y);           // Output: null
console.log(typeof y);    // Output: object (this is a long-known JavaScript bug!)

πŸ‘‰ Key Point:

  • null is a primitive data type in JavaScript.
  • The typeof null mistakenly returns "object", but it is still considered a primitive value.

Null vs Undefined Comparison

At first glance, both seem similar, but there are important differences when comparing them.

Example:

console.log(null == undefined);   // true
console.log(null === undefined);  // false
  • With loose equality (==), null and undefined are considered equal.
  • With strict equality (===), they are not equal because their data types differ.

Null vs Undefined: Comparison Table

Feature null undefined
Definition Represents an intentional absence of any object value Default value of uninitialized variables
Assigned By Explicitly set by the programmer Automatically assigned by JavaScript
Data Type Primitive (though typeof null returns "object") Primitive (its own type)
Equality (==) null == undefined β†’ true null == undefined β†’ true
Strict Equality (===) null === undefined β†’ false null === undefined β†’ false
Usage Used when you want to intentionally clear or reset a variable Used when a variable is declared but not assigned any value
typeof Result "object" (JavaScript bug) "undefined"
Best Practice Use when you want to represent “no value” explicitly Use only when checking uninitialized variables

Can You Assign Undefined Explicitly?

Yes! Although JavaScript assigns undefined by default, you can also explicitly assign it.

let z = undefined;
console.log(z);           // Output: undefined
console.log(typeof z);    // Output: undefined

πŸ‘‰ Pro Tip:
When possible, use null to represent “no value” instead of explicitly setting undefined. This makes your code more intentional and readable.

Common Interview Questions on Null vs Undefined

  1. What is undefined in JavaScript?
    A variable that is declared but not assigned a value is undefined.

  2. What will be the output of undefined == null and undefined === null?
    undefined == null β†’ true
    undefined === null β†’ false

  3. Is null an object in JavaScript?
    No. null is a primitive data type. The typeof null === "object" is a well-known historical bug in JavaScript.

  4. Can you explicitly assign undefined to a variable?
    Yes, you can assign undefined, but it’s usually better to use null when you want to reset or clear a value.

FAQs on Null vs Undefined in JavaScript

Q1. What is the main difference between null and undefined in JavaScript?

undefined is the default value for uninitialized variables, while null is an intentional assignment to indicate “no value.”

Q2. Is undefined a data type in JavaScript?

Yes. undefined itself is a type in JavaScript.

Q3. Why does typeof null return “object”?

This is a historical bug in JavaScript. Although null is a primitive type, typeof null incorrectly returns "object".

Q4. Which should I use: null or undefined?

Use null when you want to intentionally clear a value. Use undefined only when checking for uninitialized variables.

Q5. Is null equal to undefined?

null == undefined β†’ true (loose equality)
null === undefined β†’ false (strict equality)

Final Thoughts

While null and undefined may look similar, they serve different purposes in JavaScript:

  • undefined β†’ default value of uninitialized variables.
  • null β†’ explicitly assigned to represent “no value.”

Understanding their differences is not only essential for writing clean code but also a frequent topic in JavaScript interviews.