You can only create variably named variables as part of an Object
:
obj['variable_' + i] = value;
There's no equivalent for function scoped variables, although you can of course do:
function foo() {
var obj = {};
for (var i = 0; ... ) {
obj['variable_' + i] = value;
}
}
You can fake global variables (but shouldn't) by using the window
global object in place of obj
, as shown by @Jack.
Of course in most circumstances (and since you're using numbers to differentiate the variables) what you should really do is use an array:
var myvar = [];
for (var i = 0; i < n; ++i) {
myvar[i] = ...;
}