Linked Questions
357 questions linked to/from Is there a JavaScript equivalent of PHP’s “variable variables”?
428
votes
18
answers
732k
views
Use dynamic variable names in JavaScript [duplicate]
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 ...
28
votes
7
answers
44k
views
How can I refer to a variable using a string containing its name? [duplicate]
Is there a way to refer to a Javascript variable with a string that contains its name?
example:
var myText = 'hello world!';
var someString = 'myText';
//how to output myText value using someString?...
12
votes
6
answers
78k
views
JavaScript Dynamic Variable Names [duplicate]
Ok so I want to create variables as a user clicks threw the code every click adds a new variable. I am currently using jquery and javascript I can't do it server side this must be done in the browser.
...
12
votes
4
answers
16k
views
Is there a way to access a javascript variable using a string that contains the name of the variable? [duplicate]
This way I could have a function that says whatever_way_you_do_this = something. Is this possible? Basically I could tell a function which variable I want to set by giving it a string that holds the ...
4
votes
2
answers
47k
views
Using string as variable name in JS? [duplicate]
How can I make the code that follows to work?
var x = 'name';
and then, to use the value inside x, as it was a variable, and set it, so that if i want it to be NAME, i'll have the result:
var name =...
3
votes
4
answers
4k
views
How to convert array values to variables [duplicate]
I have value an array like this:
multi_arr = ["resi_spec","resi_desc"];
So each array value is considered as a variable and I want to store some value of these variables dynamically like this:
...
3
votes
5
answers
10k
views
Can I use a variable as variable name in JavaScript? [duplicate]
I am trying to use a variable as another variables name. For example, I have the following:
var dynamicname = 'name1_'
And I am trying to set it as the name of a new variable like this:
dynamicname +...
0
votes
2
answers
2k
views
Variable variables in JavaScript [duplicate]
according to my knowledge, this feature already exists in PHP. lets look at the following php code:
$color = 'red';
$$color = 'dark';
description of the feature:
Sometimes it is convenient to be ...
1
vote
2
answers
3k
views
'Dynamic' Javascript Array Name? [duplicate]
I want to name an array a random number, theoretically.
var arrayname = "foo";
(the value of arrayname) = ["1", "2", "3"]
Is this possible?
1
vote
4
answers
3k
views
Can you use template literal for function name in javascript? [duplicate]
can you use template literals to name functions dynamically
let variable = "something"
function `constant${variable}` () {
//do something
}
1
vote
3
answers
2k
views
Javascript addEventListener from a given function string name as an argument [duplicate]
How can you set an event listener to a control with a string variable name instead of function argument name?
//working method
var elementValue = document.createElement("button");
...
1
vote
4
answers
797
views
Executing a string without eval or is eval ok to use here? [duplicate]
Is there a better way of executing the string "getData" without eval or eval a good option in this case since what is being evaluated is not user generated?
object.myMainObject(Marcus)
object = {
...
-1
votes
1
answer
5k
views
How to dynamically define arrays in javascript? [duplicate]
I need to dynamically create arrays in runtime based on count. For example if I have an source array sourcearr = ['arr1','arr2','arr3','arr4'], the count of the elements of sourcearr is 4 i.e. var ...
0
votes
2
answers
4k
views
Dynamic javascript variable name using for loop [duplicate]
I have a problem. Was working on this for a long time, but really stuck. Searching in stackoverflow for similar problems did not really help me - as people have a little different issues and I could ...
1
vote
2
answers
2k
views
For Loop - Get Element by Id [duplicate]
I am trying to create a for loop that is equivalent to the the code below that way I don't have to type it all out each time.
var q1 = document.getElementById("q1").value;
var q2 = document....