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

265
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
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