4

Possible Duplicates:
Is there a way to access a javascript variable using a string that contains the name of the variable?
JavaScript: Get local variable dynamicly by name string

A simple code to illustrate my problem -

var chat_1 = "one";

var chat_2 = "two";

var id = "1";

var new = ?? variabalize( 'chat_' + id ) 

I want the variable new to be assigned the value of variable - chat_1 which is "one"

Community
  • 1
  • 1
Sussagittikasusa
  • 2,525
  • 9
  • 33
  • 47
  • 3
    Variable variables are not good practice. Consider using an array instead. – Pekka May 28 '11 at 06:55
  • 1
    Dup of [Is there a way to access a javascript variable using a string that contains the name of the variable?](http://stackoverflow.com/questions/1441532/is-there-a-way-to-access-a-javascript-variable-using-a-string-that-contains-the-n), [Javascript using variable as array name](http://stackoverflow.com/questions/952457/javascript-using-variable-as-array-name), [How can I access local scope dynamically in javascript?](http://stackoverflow.com/questions/598878/how-can-i-access-local-scope-dynamically-in-javascript) – outis May 28 '11 at 07:02
  • ... [Convert string to variable name in Javascript](http://stackoverflow.com/questions/5613834/convert-string-to-variable-name-in-javascript), [How to find JavaScript variable by its name](http://stackoverflow.com/questions/724857/how-to-find-javascript-variable-by-its-name), [how to use variable in a variable name](http://stackoverflow.com/questions/3859709/how-to-use-variable-in-a-variable-name), [Javascript: Get access to local variable or variable in closure by its name](http://stackoverflow.com/questions/2336508/javascript-get-access-to-local-variable-or-variable-in-closure-by-its-name) – outis May 28 '11 at 07:06
  • ... [Javascript Variable Variables](http://stackoverflow.com/questions/592862/javascript-variable-variables), [“Variable” Variables in Javascript?](http://stackoverflow.com/questions/5187530/variable-variables-in-javascript), [Variable assignment in JavaScript](http://stackoverflow.com/questions/1824095/variable-assignment-in-javascript), ... I give up. – outis May 28 '11 at 07:09

3 Answers3

27

Stop. Reorganise your code. If you want to select variables with a variable, then there has to be a logical grouping for them. Make it explicit.

var chat = {
    "1": "one",
    "2": "two"
};
var id = 1;
var new_is_a_keyword_and_cant_be_an_identifier = chat[id];
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • Hey thank you for replying, this looks live a very nice way of doing what I wanted. Also how do I add more "keys"/variables to this main chat variable? – Sussagittikasusa May 28 '11 at 07:07
6

This is not a good practice, but you can do it as follows:

var i=1;
//window['name' + i] will now access the variable

Source: http://www.i-marco.nl/weblog/archive/2007/06/14/variable_variables_in_javascri

Ben G
  • 26,091
  • 34
  • 103
  • 170
  • 2
    +1 But I would emphasis **NOT GOOD PRACTICE**. Most of the time code like this is a result of coming from certain "other" languages... It must be pointed out this only works on *"global variables"* and will not work for variables defined within a function. –  May 28 '11 at 06:57
2

You can use the global window object, assuming these are not defined inside a function:

var new = window['chat_' + id];
Russell Davis
  • 8,319
  • 4
  • 40
  • 41
  • 2
    They are locally scoped variables. That won't work inside a function. – Quentin May 28 '11 at 06:57
  • @Quentin They *may* be locally scoped (and the second statement holds). However, the question is vague on this and it will work for "global variables". –  May 28 '11 at 06:59
  • 2
    +1 for answering the question. Providing warnings and context is fine, but I _hate_ it when people give lectures instead of answering the question. – shanusmagnus Mar 30 '13 at 19:01