0

Am I able to use for loop to declare variables? Because declaring variables with similar one by one is not smart.

For example if I want variables like: DATA1, DATA2 and DATA3, I will normally declare it like below:

var data1 = "";

var data2 = "";

var data3 = "";

And the below is what I expect we can do in the actual code.

Using for loop and using "i" to assign number for each variable.

for(var i = 0; i < 3 ;i++){

    var data+i = "";

}

am I able to do that? I suppose the above code is not the correct way to do that but just a concept.

5
  • 4
    You should never generate variables that way. Especially when there's that sweet data structure called an array. Commented Aug 17, 2016 at 7:56
  • so how should i do this with array ? because i cannot predict how many datax i want to have Commented Aug 17, 2016 at 7:57
  • 1
    var data=[]; will give you millions of data[x] - read the documentation Commented Aug 17, 2016 at 7:57
  • Arrays don't have a fixed size in JavaScript. You can even build sparse arrays like var arr=[]; arr[55555]=""; Commented Aug 17, 2016 at 7:58
  • This is a frequent question but it's hard to find a correct QA for closing among the thousands of similar ones having so bad answers. Every time it's answered by new coders in a bad way... Commented Aug 17, 2016 at 8:01

3 Answers 3

2

You can define Object or Global variable.

var variables = {};

for (var i = 0; i < 3; i++) {

  variables['data' + i] = i;

}

console.log(variables);

// you can use it like variables.data0


// by the way you can define global variables as you want
// but note that this pollutes the global space

for (var i = 0; i < 3; i++) {
  window['data' + i] = i;
}
// so you can use it like

console.log(window.data0, ' or ', data0)

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

3 Comments

can i use for loop to call it?
like variables.data[i]
@anson920520. just try it. if it works.. doesn't matter
1

Because of the fact that javascript is dynamically typed you can do something like that. JavaScript gives you the possibility of accessing variables through a key.

var baseVariable = {};
for(var i = 0; i < 3; i++){
    baseVariable["data" + i] = "ValueForData" + i;
}

You can than access these variables pretty easy by iterating through the baseVariabel, or if you now the exact amount you can even call it like that:

baseVariable.data1

Comments

-1

You can use eval function to evaluate code from string. For example:

var a = 2; eval('alert(a)');

Take a look at assignment chain. May be is would be suitable.

var a = b = c = d = e = f = g = "";

But this ways is not a good practice. As mentioned in comments for your question better way to use array or object to store data.

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.