Undefined Vs Null
JavaScriptWeb DevelopmentTutorialProgramming Concepts

Null & Undefined

Aman SuryavanshiAman Suryavanshi
1 min read

Undefined

Undefined means "a value hasn't been assigned yet." Think of it as an empty spot in memory that exists but hasn't been filled with anything.

It occurs when:

  • You declare a variable but don't assign it a value
  • You try to access an object property that doesn't exist
  • A function doesn't explicitly return anything
  • You have a function parameter that wasn't provided
Undefined
let dog;  // declared but not initialized - value is undefined
console.log(dog);  // undefined

function bark() {
  // no return statement
}
console.log(bark());  // undefined

const pet = {};
console.log(pet.name);  // undefined (this property doesn't exist)

Null

Null means "intentionally empty or nothing." It's a deliberate assignment to represent "no value" or "empty value."

Unlike undefined, null is always explicitly assigned:

not defined
let cat = null;  // I deliberately set this to "nothing" for now

Key Differences to Remember

Think of it like this:

  • Undefined is like an empty shelf that exists but has nothing on it yet (JavaScript assigns this automatically)
  • Null is like putting a box labeled "EMPTY" on the shelf (you have to put it there yourself)

More technically:

  • typeof undefined is "undefined"
  • typeof null is "object" (this is actually a historical bug in JavaScript!)
  • null == undefined is true (loose equality)
  • null === undefined is false (strict equality)
Loading image...

Aman Suryavanshi

Aman Suryavanshi

Passionate web developer and designer with expertise in creating functional, user-centric digital experiences using modern technologies like React, Tailwind CSS, and JavaScript.

Share this article
Enjoyed it?

Let's Create Something Amazing Together!

Whether you have a project in mind or just want to connect, I'm always excited to collaborate and bring ideas to life.

Connect with me on social media

Continue the Journey

Thanks for taking the time to explore my work! Let's connect and create something amazing together.