Programming

What is the difference between null and undefined in JavaScript?

Short answer

undefined means a variable has been declared but not yet assigned a value, while null is an intentional value explicitly assigned to represent "no value."

Both represent an absence of a meaningful value, but they signal different things about why the value is missing.

undefined

let x;
console.log(x); // undefined — declared, but never assigned

JavaScript automatically assigns undefined to variables that exist but haven't been given a value yet, and it's also what a function returns by default if it has no explicit return statement.

null

let y = null; // deliberately set to "no value"

null is something a developer assigns intentionally, to explicitly represent "nothing" or "empty" — for example, resetting a variable that previously held an object reference.

A quick comparison note

Using loose equality, null == undefined is true (JavaScript treats them as similar for this comparison), but using strict equality, null === undefined is false, since they are different types under the hood.

Last reviewed: September 2026