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

280
Views
JavaScript - What is the difference between declaring a iteration variable using a let versus a var keyword inside a for-loop?

What is the difference between using a let in the body of the for loop and a var specifically in the body of a JavaScript for loop? Do they work differently and why?

Example:

Using let

for (let i = 0; i < 10; i++) {

}

Using var

for (var i = 0; i < 10; i++) {

}

Do these work the same way or differently behind the scenes, does one actually functions better?

Does this difference apply to a while loop and a do-while loop too?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

let is block scoped and var is not. With let, you cannot use i outside the forloop body. With var (function scoped or globally scoped) you can.

for (var j = 0; j < 10; j++) {

}

console.log(j);


for (let i = 0; i < 10; i++) {

}

console.log(i);

You can run the for loop with var in your chrome console, and will see that i has been attached to the window object.

about 4 years ago · Juan Pablo Isaza Report
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!