In this tutorial, you will learn about JavaScript Null vs Undefined.
Null and undefined represent empty or missing values.
Many Developers get confused between these two, so I will clear each.
What is null in JavaScript?
null is an intentional empty value. If you declare a variable and define the value null in it.
let name = null;
console.log(name); // null
Note: If you declare a null value, that means it does not have any value.
What is undefined in JavaScript?
undefined means a variable has been declared but not assigned any value.
let name;
console.log(name); // undefined
Difference between Null vs Undefined
You can clear the difference through below details.
| null | undefined |
|---|---|
| It is a Intentional empty value. | Variable not assigned. |
| Type of Null is a object. | Type of Undefined is undefined. |
| Null is declared by Developer. | Undefined is declared by JavaScript. |
| Null means value exists. | Undefined means value not exists. |
Type Checking
Now, you will check Type Checking through below code.
1. typeof null returns Object.
2. typeof undefined returns undefined.
typeof null; // "object"
typeof undefined; // "undefined"
Comparison of null vs undefined
null == undefined // true
null === undefined // false
Note:
== is used to check only value.
=== is used to check value and type.