JS function with loop in a loop iterator problem
<p>This is my code:</p>

<pre><code>function func(){
 for(i=0; i &lt; 5; i++){
   alert('B');
 }
}

for(i=0; i &lt; 5; i++){
  func();
  alert('A');
}
</code></pre>

<p>What I expected was: BBBBBABBBBBABBBBBABBBBBABBBBBA</p>

<p>but what received was just BBBBBA</p>

<p>I found out that's because there is function scope, not block scope in JS. What I'd like to know is <strong>how to preserve such a behavior</strong>. I mean to force something like block scope. Otherwise it's very easy to make really nasty bugs - e.g. while using a function somebody else wrote or the one you wrote yourself, but a few months earlier. </p>
