31
var foo = "bar"
var bar  = "realvalue";

Is it possible to print the value of bar using foo ?

1
  • 5
    I'm quite curious what the use case for this is? When would you use it? Commented Dec 18, 2012 at 15:27

8 Answers 8

44

Approach 1: global variable

var foo = "bar";
var bar  = "realvalue";
alert(window[foo]);

OR

Approach 2: namespace
Divide your js to namespaces

var namespace = {
 foo : "bar",
 bar : "realvalue"
};
alert(namespace[namespace.foo]);
2
  • 2
    I like this solution. The namespace thing, though not really a namespace but a javascript object, is really cool.
    – Nick.T
    Commented Dec 18, 2012 at 8:58
  • 2
    Out of curiosity, Isn't the first solution limited to a browser? will it work in a node.js app?
    – Eran Medan
    Commented Dec 18, 2012 at 16:29
17

Yeah you can do something like this with eval

var foo = "bar";
var bar  = "realvalue";
alert(eval(foo));

EDIT: Seems a lot of people are against using the eval() function. My advice before using it is read this question: Why is using the JavaScript eval function a bad idea?

Then once you understand the risks you can decide for yourself if you wish to use it.

5
  • 6
    It shows how to use eval in this case, whether eval is evil is another thing, its a correct and valid answer Commented Dec 18, 2012 at 8:58
  • Though I don't agree with the eval command, this solution still has it's importance because it's good to know. In some cases you can't go without it...
    – Nick.T
    Commented Dec 18, 2012 at 8:59
  • 5
    -1 I consider this to be a bad answer, because it suggests using dangerous functionalities where safer alternatives exist. If it suggested to avoid eval, or potential problems with using eval, or that those problems even exist, or better alternatives, I would remove my down-vote.
    – ANeves
    Commented Dec 18, 2012 at 16:06
  • @ANeves Added a disclaimer in the answer :)
    – cowls
    Commented Dec 18, 2012 at 20:06
  • 2
    @cowls now I only disagree with the suggestion, and don't think it is misleading any more. Down-vote removed. :)
    – ANeves
    Commented Dec 19, 2012 at 9:44
14

If it's a global variable you can use window[foo]

1
  • No it is not global variable. But I didn't knew we can use window[foo] for global variable, so +1 . Thanks. Commented Dec 18, 2012 at 8:53
10

Don't do this kind of constructs with non-global variables, just scope whatever variables you would otherwise have floating around.

var myscope = {
    bar: 'realvalue'
},
foo = 'bar';

alert(myscope[foo]);

Btw, the above doesn't rely on the default behaviour of browsers to also register global variables in the window object, making it work for things like Node.js too.

2

Global variables are defined on the window object, so you can use:

var bar = "realvalue";
alert(window["bar"]);
2
var foo = "bar";
var bar  = "realvalue";
foo=bar;
console.log(foo);
alert(foo);
1
 var foo = "bar";
 var bar  = "realvalue";
 alert(eval(foo));
0

Yes, via eval. But unfortunately, not without eval, which is a bad idea to use.

4
  • Still, Please tell me the eval solution. Commented Dec 18, 2012 at 8:49
  • I got it alert ( eval ( foo ) ); And that's what I needed. Commented Dec 18, 2012 at 8:51
  • eval is evil; Nice! Just like Uranium is evil. Commented Dec 18, 2012 at 8:58
  • 2
    @RakeshJuyal Not really like Uranium. Uranium has way more and better uses than eval().
    – PeeHaa
    Commented Dec 18, 2012 at 9:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.