0

I have the following variable:

var MyVar = "8";

I have 2 strings for example:

var foo = "My";
var bar = "Var";

Now I want to alert MyVar value, meaning to alert "8" and not "MyVar"

alert(foo + bar) // returns "MyVar"
Brandon
  • 68,708
  • 30
  • 194
  • 223
Dor Cohen
  • 16,769
  • 23
  • 93
  • 161
  • 2
    `alert(eval(foo + bar))`. But beware, this method is dangerous. For instance, imagine that either variable does not contain a part of a variable identifier, but evil code. – Rob W Mar 18 '13 at 15:57
  • `eval` is only dangerous if the page is dynamic server side, and users have a way of interacting with the content (forums, comments, etc.) because then XSS is a concern. Users always can run whatever code they wish on a site, regardless of `eval` being present or not. The only time `eval` would be dangerous is when user's content is put into a function that calls `eval` within the function, so that other users would run the malicious script when the loaded the hackers post or comment or whatever. – howderek Mar 18 '13 at 16:12

3 Answers3

5

This is a rare case where eval will be needed:

Code

var MyVar = "8",
    foo = "My",
    bar = "Var";
alert(eval(foo + bar))

Link: http://jsfiddle.net/howderek/wcVNU/

howderek
  • 2,224
  • 14
  • 23
4

Assuming it's a global variable:

alert(window[foo + bar])

But you're probably better off using objects and properties for that. Object properties can also be accessed with bracket notation:

var obj = {
    MyProp : 8
};
var foo = "My";
var bar = "Prop";
alert(obj[foo + bar]);
bfavaretto
  • 71,580
  • 16
  • 111
  • 150
1

Without changing the context to much of what you are doing you can use the eval function. However, you have to be very careful with it.

var MyVar = 8;

var foo = "My";
var bar = "Var";

alert(eval(foo + bar));

Depending on what your doing though there are a lot of ways to do this. If you assign MyVar to be part of some context such as this, or window you can simply lookup the value with the key as the variable name.

Window Context

(function () {
  window.MyVar = 8;

  var foo = "My";
  var bar = "Var";

  alert(window[foo+bar]);
})();

Function Context

new (function () {
  this.MyVar = 8;

  var foo = "My";
  var bar = "Var";

  alert(this[foo+bar]);
})();

Object Context

(function () {
  var obj = {}
  obj.MyVar = 8;

  var foo = "My";
  var bar = "Var";

  alert(obj[foo+bar]);
})();
travis
  • 8,055
  • 1
  • 17
  • 19
  • 1
    Your function context is actually global. – bfavaretto Mar 18 '13 at 16:09
  • 1
    You'r right, care to elaborate as I'm not sure why. Do anonymous functions take on global context? – travis Mar 18 '13 at 16:19
  • 2
    Inside a function, `this` will be the global object unless (a) the function is called as an object method (then `this` will be the object), or (b) the function is called as a constructor, with the `new` operator (in which case `this` will point to the new object being constructed). https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/this – bfavaretto Mar 18 '13 at 16:37