Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

undefined

BaselineWidely available

Theundefined global property represents the primitivevalueundefined. It is one of JavaScript'sprimitive 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 valueundefined.

Property attributes ofundefined
Writableno
Enumerableno
Configurableno

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. Amethod or statement also returnsundefined if the variable that is beingevaluated does not have an assigned value. A function returnsundefined ifa value was notreturned.

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.

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 useundefined and the strict equality and inequality operators todetermine whether a variable has a value. In the following code, the variablex is not initialized, and theif statement evaluates to true.

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

Note:Thestrict equality operator (as opposed to thestandard 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

Alternatively,typeof can be used:

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

One reason to usetypeof is that it does not throw anerror if the variable has not been declared.

js
// x has not been declared before// evaluates to true without errorsif (typeof x === "undefined") {  // these statements execute}// Throws a ReferenceErrorif (x === undefined) {}

However, there is another alternative. JavaScript is a statically scoped language, soknowing if a variable is declared can be read by seeing whether it is declared in anenclosing context.

The global scope is bound to theglobal object, sochecking the existence of a variable in the global context can be done by checking theexistence of a property on theglobal object, using thein operator, for instance:

js
if ("x" in window) {  // These statements execute only if x is defined globally}

void operator and undefined

Thevoid operator is a third alternative.

js
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

Browser compatibility

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp