0

I'm trying to construct the variable name and then test if it exists using a while loop but I think I'm creating it when I test for it so keep getting 'true' and the loop goes infinite.

var1 = "value1"
var2 = "value2"
var3 = "value3"

var i = 3
Logger.log(('value'+i)==true)
var i = 4
Logger.log(('value'+i)==true)

/*
var i = 1;
while (("value"+i) != null) {
Logger.log("value"+i)
i++;
}
*/

When I build the loop I want value4 to not exist and stop the loop but it doesn't. Because I've just created it's string I suppose, so how should I be formatting the test? First question here and I have searched but the 'construction' part seems to complicate things. Thanks.

  • 1
    It's not exactly clear what you're attempting to do in the snippet you've provided. `("value"+i)` will always evaluate to a non-null string, thus resulting in your infinite loop. – esqew May 20 '20 at 20:55
  • It feels like you're asking for [variable variables](https://stackoverflow.com/questions/5187530/variable-variables-in-javascript) but I'm not sure. If so - then don't do that, use an array or an object. Also, can you clarify the loop bit? I'm not sure what it tries to achieve. – VLAZ May 20 '20 at 20:59
  • The snippet is just a example of the test. I want to do some things within the loop but I need it to work first. So I want it to test 'value4' as not existing and stop the loop once it's worked with the valid ones. It won't know how many variables there are when it runs but they will be in a similar format. – Dave Reckoning May 20 '20 at 20:59

2 Answers2

0

Variable names are not strings, so your attempt to construct a string that matches the name of a variable won't evaluate that string as code.

If you want to do this, you should create an object with properties that store values, like a variable would. Then you can pass a string of the property name into the object, to retrieve the value of the property.

Also, you shouldn't check to see if the value is null as null is a valid value that a variable or property could have been set to. That check wouldn't tell you explicitly if the property was defined or not, it would only tell you if the value of the property was null. Instead, checking for the existence of the property can be done by just passing the property name into the object. If it exists, then the result is "truthy" and your if statement would proceed into the true branch. If the property doesn't exist, the return value is falsy and you'd proceed into the false branch.

let myObject = {
 value1:"test1",
 value2:"test2",
 value3:"test3"
};

for(var i = 0; i < Object.keys(myObject).length +1; i++){
  // Property names can be passed as strings using bracket notation
  if(myObject["value" + i]){
  console.log("value" + i + " exists and has a value of: " + myObject["value" + i]);
  } else {
    console.log("value" + i + " is not defined"); 
  }
}
Scott Marcus
  • 64,069
  • 6
  • 49
  • 71
0

Firstly, I'll clarify a misconception,

I want value4 to not exist and stop the loop but it doesn't. Because I've just created it's string I suppose

'value'+i doesn't create a variable. It represents the string 'value3'. In JS, strings are truthy, so you logger will print true.

Now to the question, how to check if variable exists.

The cleanest way of doing this would be to use a dictionary represented by a simple js object.

let definedVariablesDictionary = {};

definedVariablesDictionary.var1 = 'value1';

let isVarialbeDefined = variable => definedVariablesDictionary[variable] !== undefined;

console.log(isVarialbeDefined('var1')); // true 
console.log(isVarialbeDefined('var2')); // false
junvar
  • 11,151
  • 2
  • 30
  • 46