title: Javascript: Get access to local variable or variable in closure by its name
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/598878/how-can-i-access-local-scope-dynamically-in-javascript">How can I access local scope dynamically in javascript?</a>  </p>
  </blockquote>
  
  
  
  <p>Hi all.<br>
  We all know that you can access a property of a javascript object by it's name using the [] syntax.. e.g. ob['nameOfProperty'].</p>
  
  <p>Can you do the same for a local variable?  Another answer <a href="https://stackoverflow.com/questions/1920867/javascript-get-local-variable-dynamicly-by-name-string">here</a> suggested the answer is to use window['nameOfVar'].  However, this only worked for the poster as he was defining variables at window-level scope.</p>
  
  <p>I assume that this must be possible in general, as Firefox's Firebug (which I believe is written in javascript) can show local and closure variables. Is there some hidden language feature I'm not aware of? </p>
  
  <p>Specifically, here's what I want to do:</p>
  
  <pre><code> var i = 4;
   console.log(window['i']); // this works..
  
   function Func(){
       var j = 99;
  
       // try to output the value of j from its name as a string
       console.log(window['j']); // unsurprisingly, this doesn't work
   }
  
   Func();
  </code></pre>