title: Reassignment of variables not declared with var within a callback function
body: <p>Hey guys so I am just trying to understand scope of variable with and without using var in javascript and I understand so far that if a variable is declared without var that in javascript it will go up in scope until it finds a reference to that variable and if it hits the global scope then it will create the variable there. </p>
  
  <p>My question is that in this code the output of this is 0 0 1 but I dont understand where my logic is going awry. </p>
  
  <p>In my head first found is declared to be 0 and since its not declared with var that it will now be a global variable because as it goes up scope it wont be found. Currently found is 0.</p>
  
  <p>Then it will print after the first if statement. So we are at 0.</p>
  
  <p>Then, in the get current position when it says found = 1, wont it go up scope until it sees found outside that callback function in the getMyLocation function and reassign found to 1? </p>
  
  <p>Then I assume it would now print 1 then once it's outside that code block then print 1 again to get 0 1 1. I understand what I am saying is wrong I just dont know where.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>&lt;script&gt;
  function getMyLocation() {
      found = 0;
      if (navigator.geolocation) {
          console.log(found);
          navigator.geolocation.getCurrentPosition(function(position) {
              found = 1;
              console.log(found);
          });
      }
  console.log(found);
  }
  &lt;/script&gt;</code></pre>
  </div>
  </div>
  </p>