6

Possible Duplicate:
How to access javascript variable value by creating another variable via concatenation?

In PHP I can have:

$theVariable = "bigToe";
$bigToe = "is broken";

such that:

echo "my ".$theVariable." ".$$theVariable;

would display

my bigToe is broken

How would I go about doing something similar to that in JavaScript?

Community
  • 1
  • 1
Aaron Luman
  • 635
  • 1
  • 10
  • 29
  • 1
    See: http://stackoverflow.com/questions/993013/how-to-access-javascript-variable-value-by-creating-another-variable-via-concaten – Crescent Fresh Oct 31 '09 at 21:41
  • 1
    You'd be better off using an associative array for that in PHP, or an object in JavaScript. – JAL Oct 31 '09 at 21:55
  • @Code Duck: There are cases where it's useful. – Jed Smith Oct 31 '09 at 21:57
  • 2
    Don't try and implement other programming languages mistakes – cllpse Nov 01 '09 at 06:36
  • @Jed Smith - there's not anything you can do with a dynamic variable you can't do with an associative array or an object, and this method is much less maintainable. – JAL Nov 01 '09 at 21:01

4 Answers4

5

There is a pretty good write-up on Dynamic Variables in JavaScript here:

http://www.hiteshagrawal.com/javascript/dynamic-variables-in-javascript

Eli
  • 97,462
  • 20
  • 76
  • 81
3

I would use the window array instead of eval:

var bigToe = "big toe";
window[bigToe] = ' is broken';
alert("my " + bigToe + window[bigToe]);
karim79
  • 339,989
  • 67
  • 413
  • 406
  • Why would you prefer the window[] over eval()? – Aaron Luman Oct 31 '09 at 21:53
  • 1
    @baiano: Removes any security worries that are introduced with `eval`'s usage, plus referencing a dictionary is *always* faster then evaluating Javascript just to produce a solution. – Jed Smith Oct 31 '09 at 21:55
  • @baiano - simply because I always tend to avoid using `eval` if I can possibly help it. See http://stackoverflow.com/questions/86513/why-is-using-javascript-eval-function-a-bad-idea – karim79 Oct 31 '09 at 21:58
  • @Jed Smith: referencing a dynamic property of window is not always safe, particularly if it is a dynamic function call, but it is of course safer than `eval`. E.g., if "bigToe" were created by shared user data, it could be used to overwrite some important global variable. – Brett Zamir Jul 14 '12 at 15:19
1

Simply

eval("variableName")

Although you have to be sure you know the exact value your evaling as it can be used for script injection if you're passing it untrusted content

olliej
  • 35,755
  • 9
  • 58
  • 55
  • 1
    You definitely should't use eval – Fabien Ménager Oct 31 '09 at 22:01
  • 1
    @Fabien: I know that eval has a huge number of security risks, i even commented explicitly to that effect. That said the only way to achieve what was requested is eval, saying "you definitely shouldn't use eval" implies that my answer should have been "it's impossible" which is clearly wrong. – olliej Nov 01 '09 at 12:17
1

One way is to use the eval function

var theVariable = "bigToe";
var bigToe = "is broken";
console.log('my '+theVariable+' '+eval(theVariable));

Another way is to use the window object, which holds the key-value pair for each global variable. It can accessed as an array:

var theVariable = "bigToe";
var bigToe = "is broken";
console.log('my '+theVariable+' '+window[theVariable]);

Both methods print the answer to the Firebug console.

DavidWinterbottom
  • 6,420
  • 5
  • 38
  • 39
  • 2
    the "window" option is the best. If your var is not in the global scope (for example declared in a function), you should replace "window" by "this", or the containing object name – Fabien Ménager Oct 31 '09 at 22:02
  • 1
    @Fabien: if the var is not in the global scope, then using 'this' as a prefix will not help you -- the only reason that 'this' and 'window' are often interchangable is because 'this' is always the global object when a function is called without a base and window is merely an alias to that global object. – olliej Nov 01 '09 at 12:19