27

Is there a way to refer to a Javascript variable with a string that contains its name?

example:

var myText = 'hello world!';
var someString = 'myText';

//how to output myText value using someString?
1

7 Answers 7

56

You can use an eval to do it, though I try to avoid that sort of thing at all costs.

alert(eval(someString));

A better way, if you find yourself needing to do this, is to use a hash table.

var stuff = { myText: 'hello world!' };
var someString = 'myText';
alert( stuff[someString] );
Sign up to request clarification or add additional context in comments.

3 Comments

if I'd rather use the window['varname']; approach, how would I do it using with an object? var obj = {'eleID':'varname'}; for (var key in obj) { window[obj[key]]; } This doesn't seem to work :(
I keep seeing people saying to avoid using eval for this and similar questions - but why?
@Tron mainly because you don't control someString, but you are letting it to run as though you did. It could be malicious, have coding errors, or other nightmare-inducing issues. Imagine someString were "while(1){}" or a script that sent data to a malicious server.
37

If that variable is on the global scope, you can use the bracket notation on the global object:

var myText = 'hello world!';
var someString = 'myText';

alert(window[someString]);

2 Comments

Amazing answer saves the use of eval +1 wish i could + 10
So goOD! Thank you! This is what I was looking for! eval() worked for my main case, but I couldn't check if the variable I was using eval() to exercise was defined or not. typeof window[event.data.lingua_target_content_var] !== 'undefined' works when eval() threw an error!
2

Assuming this is at the top level, window[someString] === "hello world!".

Comments

2

In JavaScript, there is no standard for creating a 'Master Object', or any built-in method to access variables on the initial scope with a string reference that I am aware of.

However, if you are using JavaScript for web development on a browser, the window Object will give you complete access to your current scope. For example:

myVar = "This is my var"; 
if(myVar == window.myVar){
     /*
         This statement would return true
         Because both variables reference
         the same string;
     */
}

This method will allow you to reference any scope-variable WITHOUT making it a string, but you can also use a string as needed with the bracket ([]) Selectors.

window['myVar']

It is worth mentioning that keeping data as a variable directly on the current scope leaves it open to be re-defined by other scripts running. Your variable can be overwritten by function argument names, for loop variables, and simply by assigning a new value to that variable.

To overcome this, I suggest using an Object to store all relevant data to your application (Static And/Or OOP). Like this:

$myApp = {
     var1 : 'This is Var1',
     apply : function(){
          alert('Started!');
     }
}

//Referencing relative variables
alert($myApp.var1);

Comments

0

You can do this with eval:

var myText = 'hello world!';
var someString = 'myText';

eval('var theTextInMyText = ' + someString + ';');

alert(theTextInMyText);

The desire to do this at all usually is a "code smell". Perhaps there is a more elegant way...

Comments

0

eval will do that:

var myText = 'hello world!!';
var someString = eval('myText');

document.getElementById('hello').innerHTML = someString;

As demonstrated here.

Comments

-1
eval("alert(" + someString + ");");

would work.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.