0

I am doing the following:

var var1 = 58;

for(var i=0;i<10;i++){
 if(("var"+i) == 58) {
 console.log("they are equal");
 }
}

Could somebody explain me why ("var" + i) is not getting the value 58?

I know the first one is a variable and maybe the second is only a string, is that it? Is there any way of making this work?

I know I might be asking something quite obvious for many, but I am just starting. Any help appreciated! :)

2
  • variables cannot be accessed in javascript in this way. you can store the value in an object (the global window object, or otherwise, and then look up values that way) Commented Oct 26, 2017 at 20:39
  • Thank you for such a fast answer! I will look a each comment/answer closely (try it) and let you know. Commented Oct 26, 2017 at 20:41

3 Answers 3

2

You should probably use an object instead:

var data = {
  var1: 58
};

for(var i = 0; i < 10; i++){
 if(data["var" + i] == 58) {
   console.log("they are equal");
 }
}

UPD: @Alex suggested a variant with window instead of data, which could be treated as global object. It works in browsers, but you should know that "there is no public standard that applies to the window object" (MDN).

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

5 Comments

Or even better, just an array of 10 entries.
@dhilt, your answer indeed worked! thank you very much. Now for the sake of knowledge, maybe you know where I could read something more about it? (I will accept the answer as correct as soon as it allows me to do it).
@MrUnity Not sure if I can recommend any good link on on "how the object works" or "what is the object", it is very basic term in javascript and all you need is just to start learning from the beginning. So continue your research, ask here new questions and good luck!
@dhilt, Hahaha your answer made me laugh.Thanks, I will.
@Bergi, I will take a look to your approach too. Thanks!
1
var var1 = 58;

for(var i=0 ;i<10;i++){
    if(window["var"+i] === 58) {
         console.log("they are equal");
    }
 }

4 Comments

This won't work in NodeJs.
Thank you @Kenney. Just copied blindly. Fixed.
Of course it won't work in Node. No global window object is defined there. just wanted to explain the idea using the simplest example.
@Alex :thumbup I also made a link to your answer.
0

dhilt's answer is probably better, but maybe you don't have the ability to change the other variable.

var var1 = 58;

for (var i = 0; i < 10; i++) {
  var cur;
  try {
    cur = eval('var' + i);
  } catch (error) {
    cur = null;
  }
  if (cur === 58) {
    console.log("they are equal");
  }
}

5 Comments

Did you try running your Snippet.. It's not saying they are equal..
It's more to do with the stack overflow code runner not allowing eval, for good reason.
No, it's not that either.
Sorry @Keith. Fixed it.
Nice one,.. you can do this also without the try catch, with something like -> eval("typeof var0 === 'undefined' ? undefined : var0") typeof on an undefined won't throw error.. Obviously change the 0 for the i loop.