0

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

5
  • Seems like that is a bad idea.... Commented Oct 2, 2017 at 17:03
  • 2
    var e = []; ? Commented Oct 2, 2017 at 17:04
  • you could take an array for sequential data Commented Oct 2, 2017 at 17:04
  • var vars = { e1: '', e2: '', ...}. use vars.e1, etc. Commented Oct 2, 2017 at 17:04
  • 1
    Why do you need so many variables? Commented Oct 2, 2017 at 17:07

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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;

Comments

0

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";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.