undefined vs null

The difference between undefined and null.

Examples

Undefined Variable

A variable without a value is undefined.

let x;
console.log(x); // undefined
console.log(typeof x); // undefined

Null Value

Null represents an intentional absence of any object value.

let person = null;
console.log(person); // null
console.log(typeof person); // object

Comparison

Comparing null and undefined.

console.log(null == undefined);  // true
console.log(null === undefined); // false

Test Your Knowledge

JavaScript Quiz

No quiz available for this topic yet.