Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is undefined a property of window/global?

Tags:

javascript

It seems that the undefined is a property of window/global :

enter image description here

I always thought that undefined is, like null, a uniqe value in JavaScript.

But above code (tested in Chrome) make me confused.

Can some explain why

undefined in window

evalute to true, while

null in window

evaluate to false

like image 787
aztack Avatar asked Apr 22 '13 07:04

aztack


People also ask

What is undefined property?

The undefined property indicates that a variable has not been assigned a value, or not declared at all.

How do you find out if a property is undefined?

In a JavaScript program, the correct way to check if an object property is undefined is to use the typeof operator. If the value is not defined, typeof returns the 'undefined' string.

Is undefined an object?

Description. undefined is a property of the global object. That is, it is a variable in global scope. The initial value of undefined is the primitive value undefined .

Why is property undefined?

undefined means that the variable value has not been defined; it is not known what the value is. null means that the variable value is defined and set to null (has no value).


1 Answers

Not only undefined, but also Infinity and NaN are values of the global object, in this case, window (as of ES5.1 specification).

The fact you can't assign a value to undefined is because the property is defined with the writable attribute set to false.

null is a primitive value (as is 5) of the type Null (as is Number for 5), not a property of window.

Take a look at the annotated ES5 specification for more background on this, its quite readable!

like image 131
Nathan Avatar answered Sep 29 '22 17:09

Nathan