Proper Understanding and Usage
undefined and null Value Comparison
| Feature | undefined |
null |
|---|---|---|
| Data Type | The Undefined type
(The only value of the Undefined type) |
The Null type
(The only value of the Null type) |
| Definition | The initial value automatically assigned by the JavaScript engine to indicate that a variable has not been assigned a value | A value that indicates that a variable has been explicitly or intentionally set to have no value |
| Meaning of the Value | A variable has been declared, but no value has been assigned | A variable explicitly or intentionally indicates the absence of a value |
| Interpretation of the Value | When undefined is returned while referencing a variable, it is easy to identify that the variable has not been assigned an initial value. |
When null is returned while referencing a variable, it is easy to identify that the variable has been explicitly or intentionally set to have no value. |
| Assigning as a Variable Value | Assigning the undefined value to a variable, such as let x = undefined;, is not recommended. It not only goes against the purpose of the undefined value as a value assigned by the JavaScript engine to indicate that a variable has not been assigned a value, but can also cause confusion. |
Using the null value is recommended when developers want to explicitly indicate that a variable has no value, such as let x = null;. If a value needs to be assigned to the variable or property later, undefined is less appropriate than null. |
| Semantic Usage (Main Difference) |
The undefined value should be used to represent variables that have not been assigned a value in JavaScript. This is the very reason why the undefined value was introduced. |
Mainly used in APIs, null is used as the value returned when an object is expected but no related object actually exists. |
When Checking a Variable's Data Type with the typeof Operator |
"undefined" is returned |
"object" is returned |
When Evaluated as a boolean Type |
false |
false |
In Short
/* No value is assigned to the variable. */
let x;
console.log(x); // Returns undefined to indicate that the variable has not been assigned a value.
/* Represents that the variable
explicitly or intentionally has no value. */
let y = null;
console.log(y); // null
When Checking a Variable's Data Type with the typeof Operator
let x; // undefined value
console.log(typeof x); // "undefined"
let y = null; // null value
console.log(typeof y); // "object"
When Evaluated as a boolean Type
/* Variable x with an undefined value */
let x;
if (x) {
console.log("Evaluates to true.");
} else {
console.log("Evaluates to false.");
}
// Output: "Evaluates to false."
// Evaluate using the Boolean() function.
console.log(Boolean(x)); // false
/* Variable y with a null value */
let y = null;
if (y) {
console.log("Evaluates to true.");
} else {
console.log("Evaluates to false.");
}
// Output: "Evaluates to false."
// Evaluate using the Boolean() function.
console.log(Boolean(y)); // false
Assigning as a Variable Value
Sometimes, the undefined is assigned to a variable to indicate that it has no value.
However, it is not recommended for developers to intentionally assign the undefined value to a variable.
Not only does it go against the purpose of the undefined value as a value assigned by the JavaScript engine to indicate that a variable has not been assigned a value, but it can also cause confusion.
When developers want to explicitly indicate that a variable has no value, use null.
In JavaScript, the null value is used not only with objects but also with numbers and strings to explicitly or intentionally indicate the absence of a value. The undefined value means that no value itself has been assigned, which makes it different from null.
let x = undefined;
let x = null;
Semantic Usage
undefined
When undefined is returned while referencing a variable, it is easy to identify that the variable has not been assigned an initial value.
The undefined value should be used to represent variables that have not been assigned a value in JavaScript. This is the very reason why the undefined value was introduced.
/* Function that checks whether a value has been assigned to a variable */
let validationVar = (v) => {
if (v === undefined) {
return "No value has been assigned to the variable.";
}
return v;
};
let x;
console.log(validationVar(x)); // Output: "No value has been assigned to the variable."
null
In the specification, the null value is defined as a primitive value that intentionally indicates the absence of an object value,
but it is also used to explicitly or intentionally indicate the absence of a value for numbers and strings, not just objects.
Mainly used in APIs,
the null value is used as the value returned when an object is expected but no related object actually exists.
The following example shows a case where the null value is returned by a DOM API.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>null Value</title>
</head>
<body>
<h1>null Value</h1>
<p>
In APIs, an object is expected to be returned, but
when no related object actually exists,
a null value is returned.
</p>
<script>
const ele = document.querySelector(".get-class");
/* If no element with the class name "get-class"
exists in the HTML document,
null is returned. */
console.log(ele); // Output: null
</script>
</body>
</html>
Code Explanation
The querySelector() function returns the first element that matches the specified CSS selector within the HTML document or a specific element.
Returns null if no match is found.
As shown in the example above, APIs that work with the DOM return the null value when an object is expected to be returned but no related object actually exists.
Things to Keep in Mind
undefined
/* Variable x with a value of undefined */
let x;
if (x === undefined) {
console.log("Evaluates to true.");
} else {
console.log("Evaluates to false.");
}
// Output: "Evaluates to true."
/* Undeclared variable */
if (y === undefined) { // Uncaught ReferenceError: y is not defined
}
However, when using the typeof operator, no error occurs, as shown below.
/* Variable x with a value of undefined */
let x;
if (x === undefined) {
console.log("Evaluates to true.");
} else {
console.log("Evaluates to false.");
}
// Output: "Evaluates to true."
/* Undeclared variable */
if (typeof y === "undefined") {
console.log("Evaluates to true.");
} else {
console.log("Evaluates to false.");
}
// Output: "Evaluates to true."
typeof y === "undefined" evaluates to true.
This behavior requires special attention because an undeclared variable is evaluated in the same way as a variable that has not been assigned a value.
You may consider using a try...catch statement to determine whether a variable has been declared.
/* Variable x without an assigned value */
let x;
if (x === undefined) {
console.log("Evaluates to true.");
} else {
console.log("Evaluates to false.");
}
// Output: "Evaluates to true."
/* Undeclared variable y */
try {
y; // Attempt to access the variable
console.log("The variable has been declared.");
} catch (error) {
console.log("The variable has not been declared.");
}
// Output: "The variable has not been declared."
null
/* A variable with a null value */
let x = null;
if (x === null) {
console.log("Evaluates to true.");
} else {
console.log("Evaluates to false.");
}
// Output: "Evaluates to true."
/* An undeclared variable y */
if (y === null) { // Uncaught ReferenceError: y is not defined
}
In this case, you can check for a null
value safely as follows.
/* A variable with a null value */
let x = null;
if (x === null) {
console.log("The variable is null.");
} else {
console.log("The variable is not null.");
}
// Output: "The variable is null."
/* Check whether a variable is declared */
if (typeof y !== "undefined") {
// Check whether the variable is null
if (y === null) {
console.log("The variable is null.");
} else {
console.log("The variable is not null.");
}
} else {
console.log("The variable is not declared.");
}
// Output: "The variable is not declared."
Specifications
| Specification | |
|---|---|
undefined
|
ECMAScript® 2027 Language Specification #sec-undefined |
null
|
ECMAScript® 2027 Language Specification #sec-null-value |
Compatibility
| Value |
Desktop Chrome
|
Desktop Edge
|
Desktop Firefox
|
Safari
|
Node.js
|
|---|---|---|---|---|---|
undefined
|
1 | 12 | 1 | 1 | 0.10 |
null
|
1 | 12 | 1 | 1 | 0.10 |