PDA

View Full Version : dynamic var names


Ronen
02-13-2006, 08:07 AM
for (i=0;i<3;i++){

this["myVar" + i] = new Number(5+i);

}

is this equivalent to this:

var myVar0 = 5;
var myVar1 = 6;
var myVar2 = 7;

?

i'm gonna write a lot of code and dont want unexpected results if i'm gonna use the [] brackets.

Scottae
02-13-2006, 10:32 AM
Pretty much......you don't need to instantiate a new Number object though. You can just use number primative:

for (var i = 0; i < 3; i++)
{
this["myVar" + i] = 5 + i;
}