- What is the use of Dynamic variables? Any scenarios?
Can I attach this value to a TextField?
var data = "testVariable"; eval("var temp_" + data + "=123;"); alert(temp_testVariable);
Asked
Active
Viewed 182 times
1

Ates Goral
- 137,716
- 26
- 137
- 190

John Cooper
- 7,343
- 31
- 80
- 100
-
3None. Use hash maps (objects). – Jun 14 '11 at 12:39
-
3Why not use the slightly more acceptable `window['temp_' + data] = '123';`? – josh.trow Jun 14 '11 at 12:42
-
@josh.trow I'd say that that's a lot more than "slightly" more acceptable :-) – Pointy Jun 14 '11 at 13:01
-
@Pointy: A mere technicality :) – josh.trow Jun 14 '11 at 13:09
-
http://stackoverflow.com/questions/35221108/how-to-get-random-variables-value-in-javascript/35250618#35250618 – Niklesh Raut Feb 07 '16 at 09:17
2 Answers
6
What is the use of Dyanmic variables? Any Scenarious
For when people haven't heard of objects or arrays.
Can i attach this value to a TextField?
Yes.

Quentin
- 914,110
- 126
- 1,211
- 1,335
6
The answer to 2) is 'yes you can'.
However for 1), and overall, you probably shouldn't be using eval: Why is using the JavaScript eval function a bad idea?
As another comment and the currently upvoted answer says - either use arrays or a map.

Community
- 1
- 1

Andras Zoltan
- 41,961
- 13
- 104
- 160