Reputation: 79
Existing code:
var e1;
var e2;
var e3;
var e4;
var e5;
var e6;
var e7;
Is there a way to declare the variables that is like "var e1 to var e50" ? I have to make a bunch of variables for my program and it is tiresome to have to type each one.
Thanks
Upvotes: 0
Views: 173
Reputation:
tiresome to have to type each one
Then you need an Array
, but you must count from 0
to your length - 1
in order to use it.
This
var e = ["e1", "e2", "e3", "e4", "e5", "e6", "e7"];
is the same as this
var e = [];
e[0] = "e1";
e[1] = "e2";
e[2] = "e3";
e[3] = "e4";
e[4] = "e5";
e[5] = "e6";
e[6] = "e7";
Upvotes: 0
Reputation: 2323
Regardless of whether it's a good idea or not, the comma operator lets you declare multiple variables without repeating 'var':
var e1, e2, e3;
you can format with whitespace:
var e1,
e2,
e3;
Upvotes: 1
Reputation: 7188
JS can't loop through variable creation.
You probably just want to use an array instead:
var myThings = [];
to access a value:
myThings[0]
You can also loop through this if you need to.
Upvotes: 1