I want to create a variable with the dynamic name with javascript [closed]

-5

For a better example, I have a for, and I want every time I loop the loop, create a variable with the NOME of it plus the index of the for ex:

for(i=0;i<5;i++){
 var "variavel+i" = i
 console.log("variavel+i")
}

In this case the loop would create the variables as follows:

var variavel0

var variavel1

var variavel2

And so on. In addition to printing on the console the values 0,1,2 ...

    
asked by anonymous 04.09.2018 / 17:48

1 answer

-3

You can use objects for this:

var variables = {};
for (var i = 1; i <= 10; i++) {
  variables['variable_' + i] = 'Valor ' + i;
}

console.log(variables.variable_1);
console.log(variables);

Reference:

04.09.2018 / 18:20