17

Is there a shortcut for writing the following 100 assignments?

variable_1 = 1;
variable_2 = 2;
variable_3 = 3;

...

variable_100 = 100;

I have tried

for(var i = 1; i <= 100; i++) {
    variable_ + i = i;
}

but I get the error message "Invalid left-hand side in assignment". Any ideas?

Randomblue
  • 112,777
  • 145
  • 353
  • 547
  • 13
    Why not use an array? https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array – David Dec 15 '11 at 19:04

8 Answers8

25

Here are a few methods:

Method 1: use eval

Here is the most direct method:

for(var i = 1; i <= 100; i++) {
  eval("var variable_" + i + " = " + i);
}
variable_1; // => 1

Disclaimer for the above method: I don't think this problem is a good candidate for using eval. If you do use eval, you should never allow user input to go into what you are evaling, or you could open your site to security risks. That mistake is the main reason people say eval is evil.

Method 2: use dynamically generated object properties

This is a much, much better way:

// If you want these variables to be global, then use `window` (if you're 
// in a browser) instead of your own object.
var obj = {};
for(var i = 1; i <= 100; i++) {
  obj["variable_" + i] = i;
}
obj.variable_1; // => 1

About the note in the comment about using window to create global variables: I would recommend against this, as it is a quick way to pollute your global scope and step on variables unwittingly.

Method 3: use an array

David suggested using an array. This is another great idea, and, depending on what you are trying to do, may be preferred:

var arr = [];
for(var i = 1; i <= 100; i++) {
  arr.push(i);
}
arr[0]; // => 1
Community
  • 1
  • 1
benekastah
  • 5,651
  • 1
  • 35
  • 50
6

This will do it:

for(var i = 1; i <= 100; i++) {
    eval("variable_" + i + " = " + i + ";");
}

eval is basically evil, but for such purpose it's OK to use it. (reference)

Live test case.

Community
  • 1
  • 1
Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
  • It's important to note that `eval` itself isn't evil. Misusing `eval` is evil. `eval` is actually a powerful tool for some situations (although not necessary for this one, IMO). See my answer. – benekastah Dec 15 '11 at 19:09
  • @benekastah I agree, but looks like most JavaScript world class professional are totally against `eval` - probably because the risk of misusing it is greater than the gain. As for better alternative I also agree, but can't see any harm in having 100 variables so didn't advice against it. – Shadow The GPT Wizard Dec 15 '11 at 19:11
  • I'd say 99.99% of the cases said variables would be global so `window['variable_'+n]` would work just fine – hugomg Dec 15 '11 at 19:14
  • @ShadowWizard: I can see harm is having 100 variables. The only allowed number in computer science are 0, 1 and n+1. If you want to store 100 of something you really should be using an array. – hugomg Dec 15 '11 at 19:16
  • @missingno as for `window` you might be right, anyhow I believe what the OP gave is just example and he won't store just numbers in there. – Shadow The GPT Wizard Dec 15 '11 at 19:20
  • 1
    You are re-invoking the interpreter in a loop. Performance-wise, that's pretty bad: http://jsperf.com/eval-vs-array – Dennis Dec 15 '11 at 20:03
6

You are better off using an array

var variable = [];
for (var i=1; i <= 100; i++) {
  variable[i] = i;
}

Later, you can access the values using variable[1], variable[2] etc.

dheerosaur
  • 14,736
  • 6
  • 30
  • 31
3

If it is like that why not to define array of the objects

var a = new Array();
for(i=0;i<100;i+=)
 a[i] = i;
Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
3

Use an array:

var variable = [];

for(var i = 1; i <= 100; i++) {
    variable[i] = i;
}

By way of analogy, you'd want to use an array instead of 100 variables for the same reason you'd want

<div class="variable"></div>
<div class="variable"></div>
<div class="variable"></div>
//and so on

instead of

<div id="variable_1"></div>
<div id="variable_2"></div>
<div id="variable_3"></div>
//and so on
<div id="variable_100"></div>

Invalid left-hand side in assignment

This error gets generated because variable_ + i is an expression. The interpreter thinks you are trying to add two variables instead of concatenating a variable name and a string. An expression cannot be on the left-hand side of an assignment operation.

Dennis
  • 32,200
  • 11
  • 64
  • 79
3

Why not using an array instead like this?

<script language="javascript">
var arrayVar = new Array();

for (var i=0; i<100; i++) {
    arrayVar["variable_" + i] = i;
}
</script>
Jivago
  • 826
  • 7
  • 21
2
for(var i = 1; i <= 100; i++) {
    window["variable_" + i] = i;
}

alert( variable_50 );

alert( variable_34 );
Esailija
  • 138,174
  • 23
  • 272
  • 326
0

Assuming you're on a browser you can do:

global[variable] = 'hello'

console.log(variable) -> hello

Anthony
  • 13,434
  • 14
  • 60
  • 80