showing results for - "how to make string refer to object in javascript"
Fátima
30 Sep 2018
1// if variable is in global scope use: window || this
2var a = "hello world";
3var varName = "a";
4console.log( window[varName] ); // outputs hello world
5console.log( this[varName] ); // also works (this === window) in this case
6
7// if variable is in local scope use: eval
8function () {
9  var a = "hello world";
10  var varName = "a";
11  console.log( this[varName] ); // won't work
12  console.log( eval(varName) ); // Does work
13}