10

Imagine i had: <div id names>142,140,150</names>

could i then (in Javascript) write a forloop which declares variable names, with these values appended ie

var list = document.getElementById('names').innerHTML.Split(',');

for(i=0; i<list.Length; i++){
    var 'beginning' + list[i];
}

so i'd essentially want to create:

var beginning142
var beginning140
var beginning150

4 Answers 4

7

You can indeed:

window['beginning' + list[i]] = 'value';

Funny coincidence, I answered a very closely related question 10 seconds prior to this one, and then I used exactly this as an example. So a more elaborate explanation on why this works is available here.

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

1 Comment

+1 for good answer. Javascript handles obj.test the same way as obj["test"], and since all variables that are global is stored in the window-object you can do window["test"] = "something"; alert(test); and get an alert-box stating "something".
5

You can do something like this:

for(var i = 0; i<100; i++)
{
   eval("var beginning"+i);
}

For developers afraid from eval This is one of good articles talking about eval and how it is not an evil: http://www.nczonline.net/blog/2013/06/25/eval-isnt-evil-just-misunderstood/

I’m not saying you should go run out and start using eval() everywhere. In fact, there are very few good use cases for running eval() at all. There are definitely concerns with code clarity, debugability, and certainly performance that should not be overlooked. But you shouldn’t be afraid to use it when you have a case where eval() makes sense. Try not using it first, but don’t let anyone scare you into thinking your code is more fragile or less secure when eval() is used appropriately.

2 Comments

It works! In my opinion this solution deserves a good score, very clean and precise +1
"when you have a case where eval() makes sense" — Which isn't this one. JavaScript provides the Object data structure to handle this problem.
3

I can't add comment to your question due to low reputation score - this is not answer but advice. I think It would be better to create single array with your keys 142,140,150...

2 Comments

His question was wheater or not this was possible, not if it's recomanded.
Nonetheless. A word of warning is required, as ‘variable variables’ are almost always a dreadful mistake. Most people asking here how to use them are Doing It Wrong in a major way.
3

Why don't you define an object that has those values as an attribute on it.

For example:

var myVariable = function(id, value){
 this.Id = id;
 this.Value = value;
};

then you can just instantiate this object. I think it is less complicated and more readable in the end.

1 Comment

+1 - with the flexibility of JavaScript objects aka {}, it's easy to forget that creating new types of objects is equally easy.

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.