-1

I am an absolute javascript layman.

I made a simple for loop and wanted to output the value of i in a div.

Unfortunately, only the last value is displayed.

But why does console.log (i) show me every value of i in the console?

for (i = 0; i < 10; i++) {
  let myBlock = document.getElementById('myblock');
  console.log(i)
  myBlock.innerHTML = i;
}
<div id="myblock"/>
adiga
  • 34,372
  • 9
  • 61
  • 83
PlatoRG
  • 15

1 Answers1

2

"Unfortunately, only the last value is displayed."

Because each iteration you are setting the innerHTML to i so at the last iteration it will be i and so the last value is 10.

"But why does console.log (i) show me every value of i in the console?"

You have 10 iterations meaning it will run 10 times because of the for loop.

So it will console.log(); 10 times.