Accessing global variables via the window object

189 views Asked by At

So I know that you could previously access any variable using the variable name in a string form via the window object, like this:

variable = "test";
alert(window["variable"]); // Alerts "test"

And that works.

But when you do this, it doesn't work:

var variable = "test";
alert(window["variable"]); // Alerts undefined
  1. Why does the only difference, the var, cause it to return undefined?
  2. Is this a recent change to JavaScript?
  3. Is there a way around it? If so, what?

Also, please note that I am using version 55.0.2883.87 m of Chrome.

Thanks!

0

There are 0 answers