What is the difference between null and undefined?

  • undefined: the variable was declared but has no value, or a property or function result is missing. JavaScript sets it.
  • null: an empty value that the programmer sets on purpose.
let a;
let b = null;
console.log(a, b);
console.log(typeof a, typeof b);
console.log(a == b, a === b);

Output:

Tutorials Class - Output Window

undefined null
undefined object
true false

typeof null gives "object", which is an old bug of the language.


Learn more about the similar topics:
Tutorials
JavaScript Variables
JavaScript Scope
JavaScript Data Types
JavaScript Type Conversion
JavaScript Operators
JavaScript Numbers and Math
Exercises & Assignments
Write a JavaScript program to add two numbers
Write a JavaScript program to swap two variables
Interview Questions & Answers
What are the data types in JavaScript?
What is the difference between == and === in JavaScript?
What is NaN in JavaScript?
What is the difference between null and undefined?
What is scope in JavaScript?
What is the difference between var, let and const?
What is hoisting in JavaScript?