undefined
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
Theundefined global property represents the primitive valueundefined. It is one of JavaScript'sprimitive types.
In this article
Try it
function test(t) { if (t === undefined) { return "Undefined value!"; } return t;}let x;console.log(test(x));// Expected output: "Undefined value!"Value
The primitive valueundefined.
Property attributes ofundefined | |
|---|---|
| Writable | no |
| Enumerable | no |
| Configurable | no |
Description
undefined is a property of theglobal object. That is, it is a variable in global scope.
In all non-legacy browsers,undefined is a non-configurable, non-writable property. Even when this is not the case, avoid overriding it.
A variable that has not been assigned a value is of typeundefined. A function returnsundefined if a value was notreturned. Accessing a property that does not exist also returnsundefined. Thevoid operator always returnsundefined.
Note:While you can useundefined as anidentifier (variable name) in any scope other than the global scope (becauseundefined is not areserved word), doing so is a very bad idea that will make your code difficult to maintain and debug.
// DON'T DO THIS(() => { const undefined = "foo"; console.log(undefined, typeof undefined); // foo string})();((undefined) => { console.log(undefined, typeof undefined); // foo string})("foo");Examples
>Strict equality and undefined
You can useundefined and the strict equality and inequality operators to determine whether a variable has a value. In the following code, the variablex is not initialized, and theif statement evaluates to true.
let x;if (x === undefined) { // these statements execute} else { // these statements do not execute}Note:Thestrict equality operator (as opposed to theloose equality operator) must be used here, becausex == undefined also checks whetherx isnull, while strict equality doesn't. This is becausenull is not equivalent toundefined.
SeeEquality comparison and sameness for details.
typeof operator and undefined
typeof can also determine whether a variable isundefined:
let x;if (typeof x === "undefined") { // these statements execute}One reason to usetypeof is that it does not throw an error if the variable does not exist in the current scope.
// x has not been declared before// evaluates to true without errorsif (typeof x === "undefined") { // these statements execute}// Throws a ReferenceErrorif (x === undefined) {}It also works with variables declared withvarafter the check, because the declaration is hoisted to the top of the scope with valueundefined.
if (typeof x === "undefined") { // these statements execute}var x = 1;This technique is usually only useful for testing global variables. You can know if a variable exists at any other scope (blocks, functions, modules, etc.) just by looking at the source code. The global scope is bound to theglobal object, so checking the existence of a variable in the global context can be done by checking the existence of a property on theglobal object, such as by using thein operator:
if ("x" in window) { // These statements execute only if x is defined globally}However, none of the techniques above work if the variable is declared withlet,const, or other lexical declarations. Usingtypeof before the line of declaration still produces aReferenceError, due to thetemporal dead zone (TDZ).
if (typeof z === "undefined") { // Uncaught ReferenceError: Cannot access 'z' before initialization}let z = 1;Furthermore,let andconst declarations do not create properties on the global object, so they cannot be checked with thein operator either.
let z;if ("z" in window) { // false, even if z is declared globally with let or const}If you want to share global variables across different scripts, it is more advisable to usevar or explicitly attach them to the global object:
window.myGlobalVar = "foo";void operator and undefined
Thevoid operator can also be used to produce theundefined value. This is very commonly seen in minified code becausevoid 0 is 3 bytes shorter and cannot be overridden. You should usually avoid this pattern in your own code.
let x;if (x === void 0) { // these statements execute}// y has not been declared beforeif (y === void 0) { // throws Uncaught ReferenceError: y is not defined}Specifications
| Specification |
|---|
| ECMAScript® 2026 Language Specification> # sec-undefined> |