Definition and Usage
The null value is a value that indicates that a variable has been explicitly or intentionally set to have no value.
It is commonly used in APIs as the value returned when an object is expected but no related object actually exists.
Basic Example
The following example shows a case where a null value is returned in a DOM API.
As shown in the example below, in DOM-related APIs, an object is expected to be returned, but when no related object actually exists, a null value is returned.
<!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.
Data Type of null
The data type of a null value is Null.
However, checking the data type of a variable with the typeof operator returns "object".
Additional Explanation
The typeof operator returns a string that represents the data type of the operand.
The typeof operator returns a string representing the data type of a given variable. However, when checking a null value, it does not return "null" but instead returns "object", which can be confusing.
let x = null;
console.log(typeof x); // "object"
Therefore, since the typeof operator returns "object" for a null value, checking for null should be done using the strict equality operator (===) to compare it directly with null.
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."
Evaluating a null Value as a boolean
When a null value is evaluated as a boolean, it is evaluated as false.
let x = null;
if (x) {
console.log("Evaluates to true.");
} else {
console.log("Evaluates to false.");
}
// Output: "Evaluates to false."
// Evaluation using the Boolean() function
console.log(Boolean(x)); // false
Things to Keep in Mind
There are some things to keep in mind when working with the null value.
When the Variable Itself Has Not Been Declared
If the variable itself has not been declared, it does not return the null value.
Instead, an Uncaught ReferenceError: ... is not defined exception occurs.
The null value indicates that a variable has been explicitly or intentionally set to have no value. It does not mean that the variable itself has not been declared.
/* 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."
Differences Between null and undefined
The null value is used in JavaScript to explicitly or intentionally indicate that a variable has no value in various contexts, including not only objects but also numbers and strings.
undefined means that a value itself has not been assigned, which is different from null.
/* A variable declared but not assigned a value */
let x;
console.log(x); // undefined is returned because no value has been assigned
/* A value explicitly set by the developer to indicate the absence of a value */
let y = null;
console.log(y); // null
When comparing null and undefined, it is important to pay attention to the difference between the == and === operators.
typeof null; // "object"
typeof undefined; // "undefined"
null == undefined // true
null === undefined // false
If you need to assign a value to a variable or property later, using null is more appropriate than using undefined.
let x; // undefined
let x = null;
Proper Understanding of null
When null is returned when a variable is referenced, it indicates that the variable has been explicitly or intentionally set to have no value.
It is commonly used in APIs as the value returned when an object is expected but no related object actually exists.
Specifications
| Specification | |
|---|---|
null
|
ECMAScript® 2027 Language Specification #sec-null-value |
Compatibility
| Value |
Desktop Chrome
|
Desktop Edge
|
Desktop Firefox
|
Safari
|
Node.js
|
|---|---|---|---|---|---|
null
|
1 | 12 | 1 | 1 | 0.10 |