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

284
Visualizações
`let` vs. `const` vs. nothing in a for–of loop

What is the difference in doing the following three constructions in JavaScript:

let dd = [ 1, 2, 3, 4, 5 ];

for(const item of dd) console.log(item);
for(let   item of dd) console.log(item);
for(      item of dd) console.log(item);

It seems they all produce the exact same results so wondering if there are some subtle differences between them, especially when neither let nor const is there, specifically in the context for a for–of loop.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Here are some examples that demonstrate the difference:

let dd = [1, 2, 3, 4, 5]

// identically-named variable in the outer scope
let item = -1

for (const item of dd) {
    console.log(item) // ok
    // console.log(++item) // can't re-assign - will throw if uncommented
}

console.log(item) // still -1, as `const` is block-scoped

for (let item of dd) {
    console.log(item) // ok
    console.log(++item) // it's fine to re-assign 🎵
}

console.log(item) // still -1, as `let` is block-scoped

for (item of dd) {}

console.log(item) // 5, as we've now re-assigned the variable in the outer scope
// due to not using `const` or `let`

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