Why doesn't if (x) match if (window.x)?

Why is this:

if(x)
  alert('Available');

gives: ReferenceError: x is not defined

while it works:

if(window.x)
   alert('Available');

Does not speak:

var x = "";

is equivalent to:

x = "";

is equivalent to:

window.x = "";

if the function is out , since all the code is surrounded with(window)?



To make it clearer: I know the difference between global member variables, but I want to know why getting an undeclared variable gives ReferenceError like x;, but window.xgives undefined? Shouldn't they give undefined?

+4
source share
2 answers

window.x "works" ( if) , undefined, . . , , , , , .

, var , . .

+5

x , , window.

var x = ""; , . x = ""; , x , .

var x = ""; , .

window.x = ""; window.

, , . , , undefined.

0

All Articles