Definition and Usage
The undefined value is the initial value automatically assigned by the JavaScript engine to indicate that a variable has not been assigned a value.
In other words, before a value is assigned to a variable, the JavaScript engine assigns undefined as the variable's initial value.
Therefore, referencing a variable after it has been declared but before a value is assigned returns undefined. However, if a value is assigned to the variable, the assigned value is returned instead of undefined.
undefined in Regular Variables
let x;
console.log(x); // undefined
Assigning a value returns the assigned value.
let x;
console.log(x); // undefined
/* Assigning a value returns the assigned value. */
x = 1;
console.log(x); // 1
undefined in Functions
In JavaScript, functions can also be treated as values.
In programming languages, if a function can be treated like other values, the language is said to have first-class functions. JavaScript also treats functions as first-class functions.
If a function does not return a value using the return keyword after execution, it returns undefined.
If a value is returned using the return keyword, that value is returned.
function x(a, b) {
a + b; // No value returned via the return keyword
}
console.log(x(1, 2)); // undefined
function y(a, b) {
return a + b; // A value is returned via the return keyword
}
console.log(y(1, 2)); // 3
The same applies to arrow functions.
const x = (a, b) => {
a + b; // No value returned via the return keyword
}
console.log(x(1, 2)); // undefined
const y = (a, b) => {
return a + b; // A value is returned via the return keyword
}
console.log(y(1, 2)); // 3
undefined in Objects
Referencing a non-existent property on an object also returns undefined.
const obj = {};
console.log(obj.prop); // undefined
Creating a property and assigning a value returns the assigned value.
const obj = {};
console.log(obj.prop); // undefined
/* Create a property and assign a value */
obj.prop = 1;
console.log(obj.prop); // 1
undefined in Arrays
Referencing a non-existent element on an array also returns undefined.
In JavaScript, arrays are also objects.
Therefore, referencing a non-existent element on an array is equivalent to referencing a non-existent property on an object.
let arr = [];
console.log(arr[0]); // undefined
Assigning a value to the array returns the assigned value.
let arr = [];
console.log(arr[0]); // undefined
/* Assign a value to the array */
arr[0] = 1;
console.log(arr[0]); // 1
Data Type of undefined
The data type of the undefined value is Undefined.
In JavaScript, you can check the data type of a variable using the typeof operator.
Additional Explanation
The typeof operator returns a string that represents the data type of the operand.
let x;
console.log(typeof x); // "undefined"
This means that undefined is a value with a unique data type.
Evaluating undefined in Conditional Statements
The following example shows how the undefined value is evaluated in a conditional statement.
let x;
if (x === undefined) {
console.log("Evaluates to true.");
} else {
console.log("Evaluates to false.");
}
// Output: "Evaluates to true."
However, when the undefined value is evaluated as the boolean type, it is false.
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
Things to Keep in Mind
There are some things to keep in mind when working with the undefined value.
When the Variable Itself Has Not Been Declared
If the variable itself has not been declared, it does not return the undefined value.
Instead, an Uncaught ReferenceError: ... is not defined exception occurs.
The undefined value indicates that a declared variable has not been assigned a value. It does not mean that the variable itself has not been declared.
/* 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."
Assigning undefined to a Variable Intentionally
In some cases, undefined is assigned to a variable to indicate that it has no value.
However, intentionally assigning undefined to a variable is not recommended.
The undefined value is automatically assigned by the JavaScript engine to indicate that a variable has not been assigned a value. Assigning it manually goes against this purpose and can cause confusion.
let x = undefined;
let x = null;
If you may need to assign a value to the variable or property later, null is more appropriate than undefined.
Proper Understanding of undefined
When referencing a variable, if undefined is returned, it clearly indicates 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 reason undefined was introduced.
Specifications
| Specification | |
|---|---|
undefined
|
ECMAScript® 2027 Language Specification #sec-undefined |
Compatibility
| Value |
Desktop Chrome
|
Desktop Edge
|
Desktop Firefox
|
Safari
|
Node.js
|
|---|---|---|---|---|---|
undefined
|
1 | 12 | 1 | 1 | 0.10 |