Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

109
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda