1

I need to create javascript objects that base on user defined number. So if user defines 20, then I need to create 20 variables.

var interval_1=0, interval_2=0, interval_3=0, interval_4=0, interval_5=0... interval_20=0;

how do I do it so the name of the object can be dynamically created?

for (i=0; i<=interval; i++){
    var interval_ + i.toString() = i;
}
4
  • Do you need them to be separate variables, or will it work for you to store them as object properties? E.g. intervals.first, intervals.second, etc. Commented Aug 8, 2012 at 1:14
  • i'm not sure exactly what object properties are, but I need them to be separate variables. Commented Aug 8, 2012 at 1:43
  • 1
    The array is definitely the best way to go, but it is interesting to note that in JavaScript, you can create top-level variables dynamically. Do you want top-level variables or local variables? Are you using a browser or node.js or some other environment? Commented Aug 8, 2012 at 1:59
  • local variables, no i'm not... I see, thanks for the clarification, I can treat each item in the array as a separate object Commented Aug 8, 2012 at 5:33

4 Answers 4

4

Erm, use an array?

for( i=0; i<=count; i++) array[i] = i;
Sign up to request clarification or add additional context in comments.

1 Comment

thanks, I can treat each item in the array as a separate object and as a 'variable'
3

Use an array:

var i, count, interval = [];

// user defines count, 20 for example
count = 20;

for (i = 0; i < count; i++) {
    interval.push(i);
}

// interval[0] === 0
// interval[19] === 19
// interval.length === 20

Note, this starts the index at 0 and goes up to count - 1. Do not use i <= count unless you start i at 1.

Here is a jsFiddle to illustrate. Hit F12 to open dev tools in most browsers and look at console, or change console.log() to alert().

Link: http://jsfiddle.net/willslab/CapBN/1/

Alternatively, you could setup a single object with properties for each value:

var i, count, intervals = {};

count = 20;

for (i = 0; i < count; i++) {
    intervals["interval_" + i] = i;
}

//intervals.interval_0 === 0
//intervals.interval_19 === 19)

Link: http://jsfiddle.net/willslab/EBjx7/2/

2 Comments

thanks for the clarification, i see now why to use an array, it can be treated as a variable to store values
No problem! JavaScript also let's us add to the array dynamically, so it's pretty convenient.
3
for (i=0; i<=20; i++){
    window["interval_" + i.toString()] = i;
}

Comments

1

Javascript variables can be created by:

  1. a variable declaration, e.g. var x;
  2. assigning a value to an undeclared variable, e.g. y = 'foo';
  3. an identifier in a formal parameter list, e.g. function bar(x, y, z);
  4. using eval, e.g. eval( 'var x = 4');

If all else fails and you want say 5 variables, you can do:

var s = [];
var i = 5;
while (i--) {
  s[i] = 'a' + i;
}

eval('var ' + s.join(',') + ';');

alert(a0); // shows undefined

If a0 wasn't defined, the last step would throw a reference error.

Of course the issue you now have is how to access them. If they are created as global variables, you can use:

globalObj['a' + i];

where globalObj is usually window, however there is no equivalent for accessing function variables since you can't access their variable object.

So the usual solution is to put things into arrays or objects where you can iterate over the properties to find things you don't know the name of.

4 Comments

wow, nice, I see now that they are dynamically created, but still undefined. Is this the syntax to set them to 0? eval('var ' + s.join('') + '= 0;');
+1 because even though eval is wrong for this case and yeah close to always wrong, this is a good and thorough answer. Maybe a disclaimer on the eval would satisfy the downvoter?
@user1518600—yes, however you still have the issue of how to access them, an array or object is a better solution since variables are just properties of a variable object (which is inaccessible by design) rather than a native object.
thank you guys for the clarification and the thorough answers

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.