+ 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>
7 ответов
+ 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").
+ 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
+ 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.
+ 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
+ 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.
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