0

How can I assemble a var in Javascript during runtime without eval?

var lc = $('.bezeichnung-1').length;
for (var lt = 1; lt <= lc; lt++) {
    eval("var neuerwert"+lt+"=0;"); // this works but I don't want to use it because I read that eval is bad
}


var lc = $('.bezeichnung-1').length;
for (var lt = 1; lt <= lc; lt++) {
    window["var neuerwert"+lt] = 0; // this does not work
}
Coffeehouse
  • 505
  • 1
  • 3
  • 15
  • 2
    I have fear that something happens to me if I answer this question – svillamayor Jul 25 '13 at 12:42
  • @svillamayor I also gave up. We can't answer the same questions ten times a day – Denys Séguret Jul 25 '13 at 12:45
  • @ user: Out of curiousity, do you really want `var lc = $('.bezeichnung-1').length;` (looking up elements with the class `bezeichnung-1` and then using the resulting `length`), as opposed to `var lc = $('.bezeichnung').length-1;` (looking up elements with the class `bezeichnung` and using `length - 1`)? The starting value for `lt` (1) seems odd, too... – T.J. Crowder Jul 25 '13 at 12:52

3 Answers3

7

How can I assemble a var in Javascript during runtime without eval?

You don't, but you can make it a property of something.

If these are already at global scope, they're already properties:

var lc = $('.bezeichnung-1').length;
for (var lt = 1; lt <= lc; lt++) {
    window["neuerwert"+lt] = 0;
    // -----^ no `var` keyword
}

If they're not at global scope (good for you!), make them properties of an object, e.g.:

var neuerwert = {
    1: /*...value here...*/,
    2: /*....value here...*/
};

or an array

var neuerwert = [
    /*...value here...*/,
    /*....value here...*/
];

and then

var lc = $('.bezeichnung-1').length;
for (var lt = 1; lt <= lc; lt++) {
    neuerwert[lt] = 0;
}

Note that array indexes start at 0, so you may have to adjust lt if you're using an array.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • I am confused. They were not at global scope already. This is the first time I define them. Yet taking off the `var` works just fine. Why is that? – Coffeehouse Jul 25 '13 at 13:25
  • @user2164882: *"Yet taking off the var works just fine. Why is that?"* They must be at global scope, or you've assigned them specifically as `window` properties in other code. `window["neuerwert"+lt]` Looks up the property `window["neuerwert"+lt]` on `window`. Globals become `window` properties automatically. You can also add more by doing `window.propName = ...`. But you want to avoid that, the `window` object is overcrowded already. – T.J. Crowder Jul 25 '13 at 14:24
0

You are trying to use a var where you should be using an array I believe

var lc = $('.bezeichnung-1').length;
var neierwert = ();

for (var lt = 1; lt <= lc; lt++) {
    neuerwert[lt]=0; 
}
gbtimmon
  • 4,238
  • 1
  • 21
  • 36
0

eval is bad only in case you pass it an argument that you took from outside.

function badEvalExample(varName, varValue) {
    eval("var " + varName + " = " + varValue);
}

In this case, the function is vulnerable to erratic/malicious parameters. Your example is perfectly fine, although T.J. Crowder's window["neuerwert" + lt] solution is indeed more elegant.

I'd recommend, however, against using variable names like something1, something2, etc. If you need a list of variables of the same type, then it's better store them in an array: somethings[0], somethings[1], ...

I think the right phrasing for using eval is not it's bad but rather it should generally be avoided as long as possible.

Powerslave
  • 1,408
  • 15
  • 16