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

97
Visualizações
Can anyone tell me that why the {For} Loop in JavaScript is returning all the values including the very first element of the an array?
var arr = [1,2,3,4,5]
for (var i=0; i<arr.length; i++){
    console.log(arr[i]);
}

I ran this code on google chrome console and the output was like this:

1
2
3
4
5

In this code I can see that all the elements are been printed but the very first element of the array which is 1 should not print because the value of [i] has already been incremented to 1 and so 2 should be printed instead of 1.
Can someone tell me why is this happening ?

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

0

I think you are getting it wrong here. In for loop, we have

for(initialization, condition, change) {
 // body
}

So, it workes in this sequence

initialization -> condition -> body -> change

PS: It will go to body if condition evaluates to true

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