Linked Questions

428 votes
18 answers
732k views

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 ...
Finbarr's user avatar
  • 32.3k
28 votes
7 answers
44k views

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

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

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 ...
MetaGuru's user avatar
  • 44.1k
4 votes
2 answers
47k views

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 =...
buddy123's user avatar
  • 6,417
3 votes
4 answers
4k views

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

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

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

I want to name an array a random number, theoretically. var arrayname = "foo"; (the value of arrayname) = ["1", "2", "3"] Is this possible?
James A.'s user avatar
1 vote
4 answers
3k views

can you use template literals to name functions dynamically let variable = "something" function `constant${variable}` () { //do something }
jeff's user avatar
  • 97
1 vote
3 answers
2k views

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"); ...
Jaume's user avatar
  • 3,830
1 vote
4 answers
797 views

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 = { ...
marcus's user avatar
  • 19
-1 votes
1 answer
5k views

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

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

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....

15 30 50 per page
1
2 3 4 5
...
24