3

I have a variable with a value, let's say

var myVarMAX = 5;

In HTML I have an element with id="myVar".

I combine the id with the string MAX (creating a string myVarMAX). My question is how can I use this string to access a variable with the same name?

3
  • It's evil but you're looking for eval. ;) Commented Jun 19, 2011 at 10:52
  • I don't think that this is entirely clear. You already have a variable myVarMAX, then you combine "myVar" with MAX (what's that?) to form myVarMAX again? Why aren't you using arrays? Commented Jun 19, 2011 at 11:06
  • @Tomalak - he could have many vars with suffixes matching various IDs. Anyway, an associative array is of course how to do this Commented Jun 19, 2011 at 14:55

2 Answers 2

7

You COULD use eval, but if you have the var in the window scope, this is better

var myVarMAX = 5;
var id="MAX"; // likely not in a var

alert(window["myVar"+id]); // alerts 5

However Don't pollute the global scope!

A better solution is something like what is suggested in the link I posted

var myVars = {
  "myVarMin":1,
  "myVarMax":5,
  "otherVarXX":"fred"
} // notice no comma after the last var

then you have

alert(myVars["myVar"+id]);

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

4 Comments

Good answer. I wasn't expecting eval to be the only option. I am glad there is an unevil way to do this.
Except polluting the global scope is ALSO considered evil, so this is the lesser of two evils
Idd. I should have said 'uneviler'. :)
Or just "less evil". The proper solution here is to design your program correctly so that you do not have to do this at all. Use arrays.
-1

Since this post is referred to often, I would like to add a use case.

It is probably often a PHP programmer who gives Javascript/Nodejs a try, who runs into this problem.

// my variables in PHP
$dogs  = [...]; //dog values
$cats  = [...]; //cat values
$sheep = [...]; //sheep values

Let's say I want to save them each in their own file (dogs.json, cats.json, sheep.json), not all at the same time, without creating functions like savedogs, savecats, savesheep. An example command would be save('dogs')

In PHP it works like this:

function save($animal) {
    if(!$animal) return false;
    file_put_contents($animal.'.json', json_encode($$animal));
    return true;
}

In Nodejs/Javascript it could be done like this

// my variables in NodeJS/Javascript
let dogs  = [...]; //dog values
let cats  = [...]; //cat values
let sheep = [...]; //sheep values

function save(animal) {
    if (!animal) return false;

    let animalType = {};
    animalType.dogs  = dogs;
    animalType.cats  = cats;
    animalType.sheep = sheep;    

    fs.writeFile(animal + '.json', JSON.stringify(animalType[animal]), function (err){
       if (err) return false;
    });
    return true;

}

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.