title: How to get variables from the outside, inside a function in jQuery?
body: <p>I'm trying to figure out how can I get variables from the outside in a function, inside a function in jQuery but i get <code>Uncaught ReferenceError: myvar is not defined</code>. Is there any way of doing this?</p>
  
  <p>A simple example of my code:</p>
  
  <pre><code>$(function(){
      var myvar = "stackoverflow";
  });
  
  $(function(){
      alert(myvar);
  });
  </code></pre>
  
  <p>But when I define the variable outside the function it works:</p>
  
  <pre><code>var myvar = "stackoverflow";
  
  $(function(){
      alert(myvar);
  });
  </code></pre>
  
  <p>Why?</p>
  
  <p>Any help is appreciated! Thank you!</p>