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.

The undefined global property represents the primitive value undefined. It is one of JavaScript's primitive types.

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 value undefined.

Property attributes of undefined
Writableno
Enumerableno
Configurableno

Description

undefined is a property of the global 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 type undefined. A function returns undefined if a value was not returned. Accessing a property that does not exist also returns undefined. The void operator always returns undefined.

Note: While you can use undefined as an identifier (variable name) in any scope other than the global scope (because undefined is not a reserved word), doing so is a very bad idea that will make your code difficult to maintain and debug.

js
// 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 use undefined and the strict equality and inequality operators to determine whether a variable has a value. In the following code, the variable x is not initialized, and the if statement evaluates to true.

js
let x;
if (x === undefined) {
  // these statements execute
} else {
  // these statements do not execute
}

Note: The strict equality operator (as opposed to the loose equality operator) must be used here, because x == undefined also checks whether x is null, while strict equality doesn't. This is because null is not equivalent to undefined.

See Equality comparison and sameness for details.

typeof operator and undefined

typeof can also determine whether a variable is undefined:

js
let x;
if (typeof x === "undefined") {
  // these statements execute
}

One reason to use typeof is that it does not throw an error if the variable does not exist in the current scope.

js
// x has not been declared before
// evaluates to true without errors
if (typeof x === "undefined") {
  // these statements execute
}

// Throws a ReferenceError
if (x === undefined) {
}

It also works with variables declared with var after the check, because the declaration is hoisted to the top of the scope with value undefined.

js
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 the global object, so checking the existence of a variable in the global context can be done by checking the existence of a property on the global object, such as by using the in operator:

js
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 with let, const, or other lexical declarations. Using typeof before the line of declaration still produces a ReferenceError, due to the temporal dead zone (TDZ).

js
if (typeof z === "undefined") {
  // Uncaught ReferenceError: Cannot access 'z' before initialization
}
let z = 1;

Furthermore, let and const declarations do not create properties on the global object, so they cannot be checked with the in operator either.

js
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 use var or explicitly attach them to the global object:

js
window.myGlobalVar = "foo";

void operator and undefined

The void operator can also be used to produce the undefined value. This is very commonly seen in minified code because void 0 is 3 bytes shorter and cannot be overridden. You should usually avoid this pattern in your own code.

js
let x;
if (x === void 0) {
  // these statements execute
}

// y has not been declared before
if (y === void 0) {
  // throws Uncaught ReferenceError: y is not defined
}

Specifications

Specification
ECMAScript® 2027 Language Specification
# sec-undefined

Browser compatibility

desktop mobile server
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Bun
Deno
Node.js
undefined
Chrome – Full support
Chrome 1 (Release date: 2008-12-11)
footnote Full support
Edge – Full support
Edge 12 (Release date: 2015-07-29)
footnote Full support
Firefox – Full support
Firefox 1 (Release date: 2004-11-09)
footnote Full support
Opera – Full support
Opera 3 (Release date: 1997-12-01)
footnote Full support
Safari – Full support
Safari 1 (Release date: 2003-06-23)
footnote Full support
Chrome Android – Full support
Chrome Android 18 (Release date: 2012-06-27)
footnote Full support
Firefox for Android – Full support
Firefox for Android 4 (Release date: 2011-03-29)
footnote Full support
Opera Android – Full support
Opera Android 10.1 (Release date: 2010-11-09)
footnote Full support
Safari on iOS – Full support
Safari on iOS 1 (Release date: 2007-06-29)
footnote Full support
Samsung Internet – Full support
Samsung Internet 1 (Release date: 2013-04-27)
footnote Full support
WebView Android – Full support
WebView Android 4.4 (Release date: 2013-12-09)
footnote Full support
WebView on iOS – Full support
WebView on iOS 1 (Release date: 2007-06-29)
footnote Full support
Bun – Full support
Bun 1 (Release date: 2023-09-08)
footnote Full support
Deno – Full support
Deno 1 (Release date: 2020-05-13)
footnote Full support
Node.js – Full support
Node.js 0.10 (Release date: 2013-03-11)
footnote Full support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

See also