OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Iterator in for loop adds one more time when accessing it outside the for loop

  • Thread starter Thread starter theSpellBinder
  • Start date Start date
T

theSpellBinder

Guest
Why is 'i' equal to 3 but the last iteration of the for loop says it is equal to 2? The condition is X<3 but why is it 3?


Code:
let i = 0;
for (i = 0; i < 3; i++) {
  console.log(i, 'from for loop');
}
console.log(i);

<p>Why is 'i' equal to 3 but the last iteration of the for loop says it is equal to 2? The condition is X<3 but why is it 3?</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>let i = 0;
for (i = 0; i < 3; i++) {
console.log(i, 'from for loop');
}
console.log(i);</code></pre>
</div>
</div>
</p>
 
Top