-1

a simple for loop like this.

document is print 0 to 10

but why console is 11, not 10?

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

    console.log(i);
Malami
  • 13
  • 3
  • because the loop adds one to i and exits.... iteration i = 10.... okay, 10 <= 10.... net iteration, i = 11, 11 <= 10.... okay it is not so exit. – epascarello Feb 14 '22 at 14:33

1 Answers1

0

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.

epascarello
  • 204,599
  • 20
  • 195
  • 236