www.webdeveloper.com
Results 1 to 3 of 3

Thread: making a variable name variable

  1. #1
    Join Date
    Aug 2012
    Posts
    43

    making a variable name variable

    How can i use xyz to name a variable?

    I am trying something like this:

    var xyz = "test";

    var (xyz) = "abc";

    alert (test);

    Would have to display an alert "abc"
    When the only tool you have is a hammer, everything looks like a nail.

  2. #2
    Join Date
    Oct 2007
    Posts
    94
    You usually aren't supposed to use eval(), but here is one way:


    var xyz = "test";
    eval("var " + xyz + " = 'abc';");
    alert (test);

  3. #3
    Join Date
    Nov 2010
    Posts
    1,085
    there's almost always a better option than eval()

    is this what you are talking about?
    Code:
    var xyz = "test"; 
    
    window[xyz] = "abc";
    
    alert (test);
    (creates "test" as a global, ie, window variable)

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles