Ask coding questions

← Back to all posts
JavaScript Problem
ia13ru (43)

I have made a variable name equal to a value of another variable:

eval(x + " = " + y);

And I am trying to call it in a similar fashion, but I am not sure how to do this:

a = "x";
z = window[a];

Can you please help me?

Answered by Coder100 (16903) [earned 5 cycles]
View Answer
Coder100 (16903)

That won't work. Eval is inside of a block scope. Variables inside won't be accessible outside.

What you should do instead is an object:

let objects = {};
let x = "a";

objects[x] = 5;

also you can literally just do



RohilPatel (1534)


let x = 5;

y = x; // y is also now 5 
InvisibleOne (2683)

can't you just do x = y and z = a? Could you make your question a bit more clear