What's the difference between null and undefined?

👨‍💻 Frontend Developer 🟡 Often Asked 🎚️ Easy
#JavaScript #JS Basics

Brief Answer

undefined — default value for uninitialized variables 🆕 null — special value meaning “nothing” or “empty” 🚫

let a;        // undefined
let b = null; // null
 
console.log(a); // undefined
console.log(b); // null

Full Answer

null and undefined — like two kinds of “emptiness”: one accidental, the other intentional! 📦

undefined — Unintentional Emptiness

undefined — like a room that hasn’t been occupied yet:

// Variable declared but not initialized
let name;
console.log(name); // undefined
 
// Function returns nothing
function doNothing() {}
console.log(doNothing()); // undefined
 
// No such property in object
const user = {name: 'John'};
console.log(user.age); // undefined

When we get undefined:

  1. Declared variable but didn’t assign value
  2. Function returns nothing
  3. No property in object
  4. Parameter not passed to function

null — Intentional Emptiness

null — like an empty box that was intentionally left empty:

// Explicitly set "nothing"
let name = null;
console.log(name); // null
 
// Clear value
let user = {name: 'John'};
user.name = null; // Explicitly clear
 
// Not found
function findUser(id) {
  // If user not found
  return null; // Explicitly "not found"
}

When to use null:

  1. Want to explicitly set “empty value”
  2. Clear variable
  3. Didn’t find something
  4. Reset value

Checking Values

How to check undefined

let value;
 
// ❌ Bad — can break
if (value === undefined) { /* ... */ }
 
// ✅ Good — safe
if (typeof value === 'undefined') { /* ... */ }
 
// ✅ Even better — short
if (value === void 0) { /* ... */ }

How to check null

let value = null;
 
// ✅ Simply compare
if (value === null) { /* ... */ }
 
// ✅ Or check falsiness
if (value == null) { /* ... */ } // Works for both null and undefined

Comparing null and undefined

console.log(null == undefined);  // true (loose comparison)
console.log(null === undefined); // false (strict comparison)
 
console.log(null == 0);  // false
console.log(null == ''); // false
console.log(null == false); // false
 
console.log(undefined == 0);  // false
console.log(undefined == ''); // false
console.log(undefined == false); // false

When to Use What

Use undefined when:

// ✅ Didn't initialize variable
let name;
 
// ✅ Nothing to return
function process() {
  // Return nothing
}
 
// ✅ No property
const obj = {};
console.log(obj.nonExistent); // undefined

Use null when:

// ✅ Explicitly set "empty"
let user = null; // No user yet
 
// ✅ Clear value
user = {name: 'John'};
user = null; // Explicitly cleared
 
// ✅ Didn't find data
function findUser(id) {
  // Search in database
  if (!found) {
    return null; // Explicitly "not found"
  }
}

Common Mistakes

Mixing null and undefined

// ❌ Mistake — not understanding difference
let value1 = null;
let value2;
 
if (value1 == value2) {
  console.log('Same'); // true! But they're different things
}
 
// ✅ Correct — check precisely
if (value1 === null && value2 === undefined) {
  console.log('Different types of emptiness');
}

Using == instead of ===

// ❌ Mistake — loose comparison
if (value == null) { /* ... */ } // Works for both null and undefined
 
// ✅ Correct — strict comparison
if (value === null) { /* ... */ } // Only for null

Simple Rules

  1. undefined — “accidental” emptiness 🆕
  2. null — “intentional” emptiness 🚫
  3. undefined — default from JavaScript 🤖
  4. null — we set when needed 🙋‍♂️
  5. == — considers them equal (careful!) ⚠️
  6. === — distinguishes them precisely 🎯

Understanding the difference between null and undefined helps write more precise code! 💪


Want more articles to prepare for interviews? Subscribe to EasyAdvice, bookmark the site and improve yourself every day 💪