15.1.1 Value properties of the Global object

2010-07-03

Global has a couple of properties which are sometimes mistaken for language constructs. Variables like NaN, undefined, Infinity and functions like isNaN(), parseInt() and eval() are actually properties of the global scope. Whenever you use them, regardless of scope depth, they always have to be looked up all the way to the top of the scope chain.

This means that if any such property was redefined anywhere, that value is used instead. So you can redeclare undefined locally.

Code: (Javascript)
function(){
var undefined = 5;
var x;
alert(x === undefined); // false
}
var x;
alert(x === undefined); // true


Note that global is always the top of the scope chain. This is why you are able to refer to eval literally, instead of having to use global.eval(x).

Note that although most implementations allow you to refer to global as a literal, the specification does not seem to mention this explicitly. In browsers, window is the global object.

The following list of properties are defined by the specification:

Variables
- NaN
- Infinity
- undefined
Functions
- eval(x)
- parseInt(string, radix)
- parseFloat(string)
- isNaN(number)
- isFinite(number)
- decodeURI(encodedURI)
- decodeURIComponent(encodedURIComponent)
- encodeURI(uri)
- encodeURIComonent(uriComponent)
Objects
- Object
- Function
- Array
- String
- Boolean
- Number
- Date
- RegExp
Errors
- Error
- EvalError
- RangeError
- ReferenceError
- SyntaxError
- TypeError
- URIError
Tools
- Math
- JSON