Skip to content

null vs undefined

In one line

undefined means a variable has been declared but not assigned a value; null is an intentional absence of value.

In simple words

undefined and null both represent 'no value', but they mean different things. undefined is JavaScript's way of saying "this variable exists but has no value yet" — it is assigned automatically by the language. null is a value you assign intentionally to say "this is deliberately empty."

undefined appears when: a variable is declared but not assigned, a function has no return statement, a function parameter is not provided, or an object property does not exist. null is only present when you explicitly set it.

They are loosely equal (null == undefined is true) but strictly different (null === undefined is false). typeof undefined is "undefined", while typeof null is "object" (a historical bug in JavaScript).

Example

javascript
let a;
console.log(a);        // undefined
const b = null;
console.log(b);        // null
console.log(a == b);   // true (loose)
console.log(a === b);  // false (strict)

undefined is automatic; null is intentional. They are loosely equal but strictly different.

Common confusions

  • Confused with: null

    The difference: undefined is assigned by JavaScript when no value is given; null is assigned by the programmer to indicate intentional emptiness.

Related terms

Related Resources

Related Lessons

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors

← Back to glossary