+ 1

Is that possible to uses the value of the variables for creating other variables names?

Hey! I would like to do something in JavaScript: creating a variable what has the name of the value of another variable of value. For exemple, if I want a variable to have the name of the value of another variable, is that possible to do it? <script> var a = "Hey"; //I would like to create another variable wich have the value of the variable (a) as name. </script>

7th Jun 2017, 9:01 PM
Apple
7 Respuestas
+ 5
Oops. :) Of course should have followed the link posted by @Rose Sevenyears. Stackoverflow of course, as usual has it all covered. But at least the need to check my idea was usefull for me (myself) ☺ And one of solutions is here without any need to follow links (but there are at least two ways of "implementing" "variable variable").
8th Jun 2017, 10:53 PM
Andrew Harchenko (Tomsk)
Andrew Harchenko (Tomsk) - avatar
+ 4
Sure, var Hey = ...; Although, for numbers there's some naming restrictions. For example, you cannot name something with a number first Ex/ var 4e; // error var e4; // fine var 35; // error var lelTalk35; // fine
7th Jun 2017, 9:04 PM
Rrestoring faith
Rrestoring faith - avatar
+ 3
@Scarlite eval() must be usefull for this task I think. @Rrestoring faith It's possible in PHP and some people use it for optimizing some algorithms, I can try to search (and find again) for a nice (I thought then) example I saw some months ago.
8th Jun 2017, 10:42 PM
Andrew Harchenko (Tomsk)
Andrew Harchenko (Tomsk) - avatar
+ 3
@Scarlite Here is a small example I made and checked: a = "b"; b = "hello"; document.write(eval(a)); https://code.sololearn.com/WFtBqPj02eCL/?ref=app
8th Jun 2017, 10:47 PM
Andrew Harchenko (Tomsk)
Andrew Harchenko (Tomsk) - avatar
+ 2
That's just not what names are for. If you can do that, which I doubt you can, there's no need for it.
7th Jun 2017, 9:26 PM
Rrestoring faith
Rrestoring faith - avatar
0
Rrestoring faith: Yeah, I know this but, I would like that the javascript automatically creates a variable wich has as the value of another variable as a name
7th Jun 2017, 9:08 PM
Apple