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

279
Views
How does the event handler use the variables from the loop?

In my code, I create buttons that use the variable i in the loop declared via var keyword. Now each button will log 10, i.e. the last value of the variable. The question is, where does the variable in the handler come from? Does he pick it up from the main scope? And in the case of let, 9 scopes are created and the handler, when clicked, accesses i from each of them? I always thought that the handler remembers i at each step of the loop

<html>
    <head></head>
    <body>
        <h1>test</h1>
        <script>
            const start = () => {
                const body = document.querySelector('body');
 
                for (var i = 1; i < 10; i++) {
                  const button = document.createElement('button');
                  button.onclick = function() {
                    console.log(i);
                  }
                  console.log(i)
                  body.appendChild(button);
                }
            }
            start();
        </script>
    </body>
    
</html>
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

In Javascript variables are reference typed. So in the for loop scope there is only one i. And it is increased at every iterative. Thats why every button prints the last value.

about 4 years ago · Juan Pablo Isaza Report

0

The simple answer is due to scoping and closures. variable defined with var is function scoped and since onClick is a function, closures comes into play and at the end of loop, value to closure is 10.

variables defined with let is block scoped and hence a new scope is created for each loop with value 1.

about 4 years ago · Juan Pablo Isaza Report

0

It is classical js problem caused by function scope of 'var'. It is solved since ES6 via 'let' and 'const' because they have block scope (every {} creates scope for variables created via 'let' and 'const'). If you change iterator declaration from 'var' to 'let' it will works as expected.

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!