myGlobal.undefined in ECMAScript 262
The static value "undefined"
- Property Type
- undefined
Here are some examples where undefined
exists in the language:
- Variables which have been declared but never initialized:
var foo; var neverInitialized = (foo==undefined); //true
- Parameters of functions/methods which are declared but not passed when invoked:
function DoSomething(a){ return (a==undefined); //will be true if no parameter is passed to the function }
- Properties of objects (including indexes of arrays) which have never been set:
var person = { name:'Bob', age:40 }; var cats = [ 'Burmese', 'Himalayan', 'Tonkinese', 'Siamese' ]; var doesNotExist = (person.antennaLength==undefined); //true doesNotExist = (cats[900]==undefined); //true
- The return value of functions which do not specify a return value:
function DoSomething(){ var x=12; return; } var y = DoSomething(); var noReturnValue = (y==undefined); //true
As a property of the Global object, the undefined
value should be available for comparison. However, some ECMAScript interpretters (such as that used by IE for MacOS) do not include it. An alternative way to compare for undefined (as opposed to null
) is to use typeof
:
var foo;
if (typeof(foo)=='undefined'){
// this code will execute; typeof(undefined) is the string 'undefined'
}