The bfavaretto response is most recommended, but in case you need to include the numeric index in the variable name for some reason, you can use any object (including the global object window
) and assign it new properties dynamically:
for (var i = 0; i < 5; i++) {
window["teste" + i] = "teste é " + i;
}
alert(teste3); // "teste é 3"
var testes = {};
for (var b = 0; b < 5; b++) {
testes["teste" + b] = "teste";
}
alert(testes.teste3); // "teste"
(in the case of assigning to window
, it becomes a global, in the other case, you need to prefix with the created object; there are no means of creating a local variable in this way)
Note that is not a good practice , among other things because you can not iterate over your created variables easily (as you would if you used an array). I'm just responding literally to what was asked.