- Global variables are in fact properties of the global object. In web pages the global object is window.
Something about “undefined”
A variable that doesn’t have an assigned value is of type undefined. And evaluating such a variable will return undefined.
What is undefined? There’s an answer on MDC.
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.
I’m a bit confused. It’s a property of the global object, so I can change its value at any time.
document.write(a === undefined); // true
undefined = 3;
document.write(a === undefined); // false
Pity it’s not a keyword like “null”. So don’t rely on it. In my eyes, it’s no different with the variable that doesn’t have an assigned value. See the following code.
document.write(a === b); // true
document.write(a === b); // false
I don’t know whether this is described in ECMA specification or not, but I think it would be clearer if undefined was a keyword as null.
So if you really want to test if a variable is undefined, use the typeof operator instead.
typeof a == "undefined"
It’s really a mess.