title: Access a variable outside its loop with Javascript
body: <p>I need to access a variable inside a loop in another function, with Javascript (not jQuery or other js frameworks).</p>
  
  <p>Here is my js:</p>
  
  <pre><code>for (x in drinks) {
      for (y = 0; y &lt; drinks[x].length; y++) {
      // testVarGlobal = drinks[x][y].name; doesn't work
          if (drinks[x][y].type == 'type') {
  
              txt['type'] = '&lt;span&gt;' + drinks[x][y].name + ' - ' + drinks[x][y].price +'&lt;/span&gt;';
              document.getElementById("type").innerHTML += txt['type'];
  
          } // endif
      } // end for
  } // end for in
  
  function foo() {
      // on the next line I need the value from the loop - drinks[x][y].name
      var something = document.getElementById(testVarGlobal);
      return something.value;
  }
  </code></pre>