I know it's possible in PHP to have "variable" variables. For example
$x = "variable";
$$x = "hello, world!";
echo $variable; // displays "hello, world!"
Is this possible in javascript? How would it be done?
I know it's possible in PHP to have "variable" variables. For example
$x = "variable";
$$x = "hello, world!";
echo $variable; // displays "hello, world!"
Is this possible in javascript? How would it be done?
If you are desperate to do this you can either try using eval():
Or using the window object:
http://www.hiteshagrawal.com/javascript/dynamic-variables-in-javascript
You can use the JavaScript
eval(str)
function.What this function does is convert the string provided into JS code, then executes it.
For example:
So to use it as a variable variable, you can do the following:
This will produce the exact same output as:
(Example is taken from the PHP manual on variable variables.)
Unlike PHP, JavaScript doesn't offer access to the globals array (which contains references to all the variable names currently declared). As such, JavaScript does not offer native support for variable variables. You can, however, emulate this feature as long as you define all your variables as part of an array or an object. This will in turn create a gloabls array for you. For example, instead of declaring the variable
hello
in the global scope like this:let's encapsulate it inside an object. We'll call that object vv (variable variables):
Now we can refer to the variable by it's index, and since array indexes can be provided using a variable we are de facto making use of a variable variable:
The Answer To Your Question
Let's apply this to the code you supplied in the original question:
A Practical Use
While the previous code might appear ridiculously cumbersome and impractical, there are practical uses for variable variables in JavaScript using this type of encapsulation. In the example below we use the same concept to get the ID of an undefined number of HTML elements.
This example declares variable variables (keys in
elementIds
) based on the ID of each element, and will assign the node of said element as the value of each variable. And since using global variables in JavaScript is generally discouraged giving your variable variables a unique scope (in this instance, declaring them inside theelementIds
array) is not only neat, but also more responsible.To reference a variable in javascript with only a string, you can use
You can set and reference variables, and objects in variables too.
There is no single solution for this (well, there is
eval
, but lets not seriously consider that). It is possible to access some global variables dynamically viawindow
, but that doesn't work for variables local to a function. Global variables that do not become a property ofwindow
are variables defined withlet
andconst
, andclass
es.There is almost always a better solution than using variable variables! Instead you should be looking at data structures and choose the right one for your problem.
If you have a fixed set of names, such as
store the those name/values as properties of an object and use bracket notation to look them up dynamically:
In ES2015+ it's even easier to do this for existing variables using concise property notation:
If you have "consecutively" numbered variables, such as
then you should be using an array instead and simply use the index to access the corresponding value: