undefined

Table of contents

  1. 1. Summary
  2. 2. Syntax
  3. 3. Description
Table of contents
  1. 1. Summary
  2. 2. Syntax
  3. 3. Description

Summary

The value undefined.

Core Global Property
Implemented in JavaScript 1.3
ECMAScript Edition ECMAScript 1st Edition

Syntax

undefined

Description

undefined is a property of the global object, i.e. it is a variable in global scope.

The initial value of undefined is the primitive value undefined.

JavaScript 1.8.5 note

Starting in JavaScript 1.8.5 (Firefox 4), undefined is non-writable, as per the ECMAScript 5 specification.

A variable that has not been assigned a value is of type undefined. A method or statement also returns undefined if the variable that is being evaluated does not have an assigned value. A function returns undefined if a value was not returned.

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 defined, and the if statement evaluates to true.

var x;
if (x === undefined) {
   // these statements execute
}
if (x !== undefined) {
   // these statements do not execute
}
Note: The strict equality operator rather than the standard equality operator must be used here, because x == undefined also checks whether x is null, while strict equality doesn't. null is not equivalent to undefined. See comparison operators for details.

Alternatively, typeof can be used:

var x;
if (typeof x == 'undefined') {
   // these statements execute
}

Tags (1)

Edit tags

Attachments (0)

 

Attach file