Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

90
Vistas
Why do for loops initialize a new value when the iterator is declared with let?

In this classic interview question, a new i is created for each iteration. This new i has closure with the function in setTimeout and produces the output 0 1 2 3 4. I don't understand why a new i is initialized when it's declared with let but not when it's declared with var. That should be impossible since let doesn't allow another variable with the same name to be created. Also, since closures are created when a function is defined, shouldn't the anonymous function in setTimeout still have closure around the original i equal to 0 in the block scope? Thanks.

for (let i = 0; i < 5; i++) {
    setTimeout(function () {
        console.log(i);
    }, 1000);
}
//Output: 
//0 
//1
//2
//3
//4
about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda