Can I use a variable to name other variables?

Can you use variables in a variable name in Javascript?

  • var array1 = new Array(4); var array2 = new Array(4); var array3 = new Array(4); var array4 = new Array(4); var array5 = new Array(4); var array6 = new Array(4); var array7 = new Array(4); for(var a = 1; a < 8; a++){ array+ a = new Array(4); } I want to make an array with a for loop, but the variables has to be diffrent every time. So my question if this is possible, and if it is, how?

  • Answer:

    No you can't (not unless you use Eval() which you shouldn't...). As Justinas has commented, you could use a Multidimensional array. var array = []; array.push(new Array(4)); array.push(new Array(4)); array.push(new Array(4)); array.push(new Array(4)); array.push(new Array(4)); array.push(new Array(4)); array.push(new Array(4)); for(var a = 1; a < 8; a++){ array[a] = new Array(4); }

Twan Korthout at Stack Overflow Visit the source

Was this solution helpful to you?

Other answers

You can do like so: var myArrays = {}; myArrays["arrA"] = new Array(5); myArrays["anotherArr"] = new Array(8); ...

Galman33

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.