Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

About for loop result why out of block console will over loop range?

a simple for loop like this.

document is print 0 to 10

but why console is 11, not 10?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

for(var i=0; i<=10; i++){
      document.write(i);
    }

    console.log(i);

>Solution :

Because the loop adds one stores the value. It then checks the condition after it incremented the value. It the condition is met, it enters the loops body. If it is not met, it exits out.

iteration i = 10, 10 <= 10…. okay, enter loop’s body

next iteration, i = 11, 11 <= 10…. okay it is not so exit the loop.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading