JavaScript's undefined Defined

©2011, Martin Rinehart

It seems pretty simple, until you start to think about it. What is JavaScript's undefined?

In turns out that there are at least four (maybe five) valid answers. In fact, if you omit any one of the answers you haven't really understood undefined.

undefined Is a Value

Here are some ways to create a name, as in a name/value pair, the value of which is undefined.

var x; // value undefined

arr = new Array( 10 );
arr[ 0 ]; // value undefined

obj = { name1: 'one', name2: 'two' };
obj[ 'name3' ]; // value undefined

function no_return() { /* no return */ }
x = no_return(); // value undefined

undefined Is the Name of a Window Property

At first, undefined was truly undefined. To clarify, ECMA specified that it must be the name of a global variable, a property of a window object in a browser: window.undefined.

That left the world wide open for jokes (possibly unfunny) such as this:

    window.undefined = "Surprise!";

Subsequent correction by ECMA declared window.undefined to be a read-only property, which is where we are today.

undefined Is a Simple Data Type

Let's clarify this. Each data element has three parts.

  1. Name
  2. Type
  3. Value

Consider three examples.

    var x = 3,
        y = 'defined',

Everything in JavaScript is built on name/value pairs. As you know, the names of the three variables in the example above are x, y and z, the names of three properties of a window object in a browser. (People often write about "the" global object, which is a bit sloppy. If you launch a second window, you have two global objects, both named "window". If you use frames you probably have more.)

You can see the names, but you cannot see the actual values, which are bit patterns that make sense to the computer. The digit "3" is used by us as a convenient, human-readable notation for a pattern of bits that is, in JavaScript, specified by the IEEE 64-bit number standard. Each bit pattern has a type. In this case, it is a number. The type tells JavaScript how to interpret the bit pattern.

Similarly, the word defined that is the value of y is also an internal bit pattern. It is probably a byte-array for the ASCII representation of ['d', 'e', 'f', 'i', 'n', 'e', 'd']. An internal dictionary will specify the size and address of this byte array. Its type is string. A byte array that is 64 bits long (eight bytes) is probably a valid number, too. JavaScript looks at a value's type to know, among other things, whether it is a number or a string.

What is the value of z? It is a special value called "undefined". Like numbers and strings, it is an internal bit pattern, in this case chosen by the JavaScript implementors. We know when it is assigned (in the above example, when a var is declared but not initialized). We don't know what the bit pattern is, nor do we care. If it is necessary for JavaScript to show this value to a human, it shows it as the string "undefined". If JavaScript needs to use that value it takes care. For example, 2 + undefined is another special value, shown to humans as "NaN". JavaScript avoids inadvertently adding a bit pattern's numeric value to a number because it checks on the type of the value. In the case of undefined the type is undefined.


Suppose you want to create a property prop to augment some object obj but only if there isn't already a property named "prop". This way?

if ( obj.prop === undefined ) ...

The above fragment is not what you want. It does two tests. First, it checks to see if obj.prop is undefined. That's the test you want. Second, when obj.prop is defined, it checks to see if the value of that property is undefined. If you don't want to replace existing properties, regardless of value, use:

if ( prop in obj ) ...

To check for existence in the object, but not in the object's prototype chain, use:

if ( obj.hasOwnProperty('prop') ) ...

Now let's look at some more meanings of undefined.

The String Representation of undefined

JavaScript has only a handful of types: boolean, number, string, null, undefined and object. Do you think that somewhere internally, next to x (remember x? as in var x = 3;?) JavaScript stores the word number? I don't. With fewer than 8 separate value types, you could encode the type in just three bits: 000–boolean, 001–number, and so on. Each of these types has a name, a character string that JavaScript shows to us if we ask, for example, alert( typeof x );.

So when we ask, alert( typeof z ); JavaScript will look up the type (maybe a 3-bit code) and return a character string: undefined.

Note that this goes for both the type and the value of undefined. Let's ask for a bit more: alert( x + ', ' + typeof z );. That should tell us: undefined, undefined. If the computer were a bit more clever at conversation, it might say, "The value of x is undefined and the type of z is undefined." So "undefined" is the string representation of the undefined type and the undefined value. You choose whether that is one answer or two.

A Good English Word, Too!

"Undefined" is also a good English word describing, for example, "flooglebop," a word I just made up. If you look for "flooglebop" in your dictionary, you won't find it. It's undefined.

The original question, however, was about "JavaScript's undefined." Flooglebop is not part of JavaScript, so flooglebop is not part of the answer to the question.

I bring this up because some browsers use the word "undefined" when speaking of a ReferenceError, an attempt to use a variable name that has not been declared (explicitly or implicitly). Browser error messages may not have been given as much thought as they deserve.

If you just wanted to know what undefined was in JavaScript, you've got your answer. Bail out here. If you refuse to accept any answer without asking, "Who says so? Can you prove it?" the last section is for you.

Some References

Let's look at the latest ECMA 262 standard (5.1 Edition / June 2011) and books by two widely respected authors, Douglas Crockford (JavaScript: The Good Parts) and David Flanagan (JavaScript: The Definitive Guide).

ECMA-262 5.1

You might want a single, definitive source for discussions of JavaScript's finer points. Discussions such as this short article. Sorry.

First, 262 is written primarily for implementors who write the JavaScript language internals, not for those of us who write JavaScript programs. Can you read it? Yes, but only if you have tremendous diligence, determination and time on your hands. Second, implementors often do things that are not part of the standard. Sometimes this is good. I like innerHTML (widely implemented before it was made part of the standard). Authors such as Crockford and Flanagan describe JavaScript as it exists, not just as the standard specifies.

With those warnings, 262 says plainly that undefined is a type, a read-only property of the global object, and a type with exactly one value. Its value is undefined. For more, refer to the standard itself. Warning: undefined appears in 253 places, not just in one.


Mercifully, The Good Parts is indexed where you can find 10 references to undefined. In page number order they say:

Clearly, Crockford focuses on undefined as a value, although on page 20 he also recognizes it as a type.


The Definitive Guide also has ten references to undefined in its index, beginning with page 20 which says, "JavaScript also defines two trivial datatypes, null and undefined, each of which defines only a single value." The other nine references also speak of undefined as both type and value. One also points out that undefined == null but undefined !== null. Equal, yes. Identical, no. Another reason, if you need one, to use === for all your comparisons.

Feedback: MartinRinehart at gmail dot com

# # #