title: Why is the variable value being passed here
body: <p>I wrote the following code with two functions <code>a()</code> and <code>b()</code>.</p>
  
  <p>The function <code>a()</code> has a loop as shown below and within that loop it calls the function <code>b()</code>. The problem is that the loop counter of the first function gets its value from <code>b()</code> and then stops. No values are being passed between the two functions and the only thing that they have in common is the name of the variable.</p>
  
  <p>Can anyone explain what exactly is happening here?</p>
  
  <p>As per my understanding, they both should have their own copy of <code>x</code>, unless I declare <code>x</code> to be global.</p>
  
  <p><a href="http://jsfiddle.net/rahulkadukar/s2gs9j05/" rel="nofollow">http://jsfiddle.net/rahulkadukar/s2gs9j05/</a></p>
  
  <pre><code>a();
  
  function a() {
      for (x = 0; x &lt; 4; x++) {
          console.log(x); 
          b();
          console.log(x);
       }
  }
  
  function b(){
      for (x = 0; x &lt; 4; x++) {
          //Do nothing 
      }
  }
  </code></pre>
  
  <p>The output of the above function is</p>
  
  <blockquote>
    <p>0<br>
    4</p>
  </blockquote>