+ 1

Is it possible to do this?

Is it possible to do this in order to use a different variable name to set the same value everytime i call a function? var x; function test(){ x++; var "box"+x=something; alert("box"+x); }

27th Nov 2016, 11:53 AM
Foxtrot Pipe
Foxtrot Pipe - avatar
3 odpowiedzi
+ 4
Yes, you can use as an object. Stop. Reorganise your code. If you want to select variables with a variable, then there has to be a logical grouping for them. Make it explicit. var id = 3; var chat = { "1": "one", "2": "two" }; function variable_generator(value) { chat[id++] = value; } Add a new data will like this variable_generator("data"); and than test your variable. alert(chat[3]);
27th Nov 2016, 12:20 PM
Aditya kumar pandey
Aditya kumar pandey - avatar
+ 1
You can use the fact that in JS, objects can be called as arrays and do that using an object. var object = {}; var x = 0; function test() { x++; object["box" + x] = something; alert("box" + x); } Of course, it won't be directly the variable name, but I don't think you can do as you wanted.
27th Nov 2016, 12:11 PM
Pierre Varlez
Pierre Varlez - avatar
0
Y U NO CODE
28th Nov 2017, 5:28 PM
Foxtrot Pipe
Foxtrot Pipe - avatar