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

153
Vistas
Difference in output between While and For loop JavaScript - Newbee

It's a basic question; I can't seem to understand why in the code below the while loop output begins with 2 "#" and the for loop start with 1

Console Log Screenshot


let brick = "#";
while (brick.length < 8){

 brick += "#";
  console.log(brick);
}
console.log("For Loop");
for (let i = "#"; i.length <8; i+="#"){
 console.log(i) 
}

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Because in the while loop, you are incrementing before and in the for loop after writing to the console.

Move your brick += "#"; to the end of the while loop and they will behave both the same way:

let brick = "#";

while (brick.length < 8) {
    console.log(brick);
    brick += "#";
}

For-Loop:

for (let i = "#"; i.length < 8; i += "#")
                                  ^ happens AFTER each iteration
about 4 years ago · Juan Pablo Isaza Denunciar

0

You have already instatiated the brick at the start. But, when you are looping in the for loop the length is increased after the loop is finished. And the opposite is happening in the while loop.

about 4 years ago · Juan Pablo Isaza Denunciar
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