Use dynamic variable names in JavaScript

In PHP you can do amazing/horrendous things like this:

$a = 1;
$b = 2;
$c = 3;
$name="a";
echo $$name;
// prints 1

Is there any way of doing something like this with Javascript?

E.g. if I have a var name="the name of the variable"; can I get a reference to the variable with name name?

17 Answers
17

Leave a Comment