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

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

Rakesh Juyal
  • 35,919
  • 68
  • 173
  • 214

8 Answers8

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]);
Swatantra Kumar
  • 1,324
  • 5
  • 24
  • 32
Akhil Sekharan
  • 12,467
  • 7
  • 40
  • 57
  • 2
    I like this solution. The namespace thing, though not really a namespace but a javascript object, is really cool. – Nick.T Dec 18 '12 at 08:58
  • 2
    Out of curiosity, Isn't the first solution limited to a browser? will it work in a node.js app? – Eran Medan Dec 18 '12 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.

Community
  • 1
  • 1
cowls
  • 24,013
  • 8
  • 48
  • 78
  • 6
    It shows how to use eval in this case, whether `eval` is evil is another thing, its a correct and valid answer – Moritz Roessler Dec 18 '12 at 08: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 Dec 18 '12 at 08: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 Dec 18 '12 at 16:06
  • @ANeves Added a disclaimer in the answer :) – cowls Dec 18 '12 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 Dec 19 '12 at 09:44
14

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

NullPoiиteя
  • 56,591
  • 22
  • 125
  • 143
Moseleyi
  • 2,585
  • 1
  • 24
  • 46
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.

Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
2

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

var bar = "realvalue";
alert(window["bar"]);
palaѕн
  • 72,112
  • 17
  • 116
  • 136
2
var foo = "bar";
var bar  = "realvalue";
foo=bar;
console.log(foo);
alert(foo);
Balwinder Pal
  • 97
  • 2
  • 10
1
 var foo = "bar";
 var bar  = "realvalue";
 alert(eval(foo));
user1331
  • 38
  • 7
0

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

Community
  • 1
  • 1
Kilian Foth
  • 13,904
  • 5
  • 39
  • 57