Sitemap
Press enter or click to view image in full size

JaJavaScript Null vs. Undefined

--

Understanding the distinction between null and undefined is fundamental to mastering JavaScript's type system and memory management.

1. Core Definitions

Undefined

undefined means a variable has been declared but has not yet been assigned a value. It is the default state provided by the JavaScript engine.

  • Analogy: A box that has been labeled but is currently empty.

Null

null is an assignment value. It represents the intentional absence of any object value. It must be explicitly assigned by the developer.

  • Analogy: A box that has been labeled and then deliberately emptied or marked as “none.”

2. Technical Comparison Table

FeatureundefinednullMeaningUnintentional absence (Missing).Intentional absence (Empty).Type (typeof)"undefined""object" (Legacy bug).Arithmetic contextBecomes NaN.Becomes 0.Equality (==)null == undefined is true.null == undefined is true.Strict Equality (===)null === undefined is false.null === undefined is false.

3. Common Scenarios

When you will see undefined:

  1. Uninitialized variables: let x;
  2. Missing function arguments: When a caller doesn’t provide a value for a parameter.
  3. Missing object properties: Accessing user.age when age doesn't exist.
  4. No return statement: A function that completes execution without a return keyword.

When you should use null:

  • When you want to explicitly indicate that a variable has no value
  • When you want to intentionally clear or reset a variable
  • When you are fetching data and want to explicitly show that a value is currently “empty” or “not found” (as opposed to simply being missing).
let user = null;
// User is intentionally empty (not logged in yet)
fetchUser().then(res => {
user = res ?? null;
// null means "user not found"
});

4. Interview “Deep Dive” Points

The typeof null Bug

If an interviewer asks why typeof null is "object", explain that it is a legacy bug from the first version of JavaScript. In the original implementation, values were stored in 32-bit units. The type tag for objects was 0, and since null was represented as a null pointer (all zeros), it was mistakenly flagged as an object.

Press enter or click to view image in full size

The Nullish Coalescing Operator (??)

Modern JS uses the “Nullish” concept to group these two together.

let settings = user.theme ?? 'default';
// Returns "default" ONLY if user.theme is null or undefined.

5. Quick Reference Code Snippet

let a;
let b = null;
console.log(typeof a); // "undefined"
console.log(typeof b); // "object"
console.log(a + 10); // NaN (undefined is not a number)
console.log(b + 10); // 10 (null is treated as 0)

Get Nazmul Hussain’s stories in your inbox

Join Medium for free to get updates from this writer.

console.log(a == b); // true
console.log(a === b); // false

6. The Null/Undefined Type Paradox

a. Is undefined a type?

Yes. undefined itself is a data type. There are 7 primitive types (String, Number, BigInt, Boolean, Undefined, Symbol, and Null) in JavaScript.

b. Is null a primitive or non-primitive ( As it is an object )

  • Technically, It is a Primitive.
  • The typeof quirk: typeof null returns "object".
  • The Reason: This is a legacy bug from JS version 1. Objects were tagged with 0 in memory, and null (the null pointer) was also represented as 0.
  • Interview Tip: Always clarify that while typeof says object, null is fundamentally a primitive because it is immutable and lacks properties or methods.

7. Interview Questions & Answers

Q1. What is the difference between null and undefined?

Answer:
undefined means a variable exists but has no value assigned, while null is intentionally assigned to represent no value.

Q2. Who assigns undefined?

Answer:
The JavaScript engine automatically assigns undefined.

Q3. Why does typeof null return "object"?

Answer:
Due to a historical bug in JavaScript where null was stored as a null pointer with the same internal tag as objects.

Q4. Is null a primitive type?

Answer:
Yes. null is a primitive, despite typeof null returning "object".

Q5. Why does null == undefined return true?

Answer:
Loose equality (==) performs type coercion and treats both as equivalent empty values.

Q6. Why does null === undefined return false?

Answer:
Strict equality (===) checks both value and type, and they are different.

Q7. What happens when you perform arithmetic with null and undefined?

Answer:
null is treated as 0, while undefined results in NaN.

Q8. When should you use null instead of undefined?

Answer:
When you want to explicitly represent an empty or reset state.

Q9. What is the nullish coalescing operator (??)?

Answer:
The ?? operator returns a fallback value only when the left operand is null or undefined.

Q10. Is undefined mutable?

Answer:
No. undefined is a primitive and cannot be modified.

Q11. Why does null + 1 equal 1, but undefined + 1 equals NaN?

Answer:
When coerced to a number, null becomes 0.
undefined cannot be converted to a number, so it becomes NaN.

Q12. How do you check if a variable is null safely?

Answer:
Use strict equality:

if (value === null) {
// handle null
}

--

--