I have the following issue:
const value1 = "some value";
var value2 = "some value";
console.log(window["value1"]); // undefined
console.log(window["value2"]); // some value
I know that const is block scoped and that is the reason I cannot access it through the window object. My question is, is there any way to access a const variable using only a string identifier.
If all I have access to is "value1", how do I get a handle on the actual value1? Is it even possible?
eval
, and yes you should not do that. Why not simply doconsole.log(x == "value1" ? value1 : …)
? Why do you even need to access local variables by string, why don't you use an appropriate data structure (like aMap
or an object)? – Bergiconst constants = { value1: "some value" };
Then you can get it usingconsole.log(constants["value1"])
. – Heretic Monkey