Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

108
Views
Question about syntax in JS I did not understand - for loop and set time out

for loop and setTimeout with VAR

The Code is:

for (var i = 0; i < 5; i++) {
  setTimeout(() => {
    console.log('The index of this number is: ' + i);
  }, 0);
}

And the output is 5 times the line

The index of this number is: 5

for loop and setTimeout with LET

The Code is:

for (let i = 0; i < 5; i++) {
  setTimeout(() => {
    console.log('The index of this number is: ' + i);
  }, 0);
}

The output is:

The index of this number is: 1
The index of this number is: 2
The index of this number is: 3
The index of this number is: 4
The index of this number is: 5

Why the output is different ? Why does this work like this ?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!