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

130
Vistas
Postfix Increment in While Loop Javascript

Could anybody explain to me please why this loop

let i = 0
while (i < 5) {
    i++
    console.log(i)  
}

shows 1,2,3,4,5 ?

As I know postfix increment returns the old value, right? so first shouldn't it log 0 into the console?

Thank you

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

0

That is true, when executing as one statement. You have separated it in 2 lines, so 2 statements, each one is executed sequentially


The following code would do it

let i = 0
while (i < 5) {
    console.log(i++)  
}
console.log("===============")
i = 0
while (i < 5) {
    a = i++
    console.log(a)  
}


Whereas the prefix increment does 12345

let i = 0
while (i < 5) {
    console.log(++i)  
}
console.log("===============")
i = 0
while (i < 5) {
    a = ++i
    console.log(a)  
}

about 4 years ago · Juan Pablo Isaza Denunciar

0

Before printing to the console, the code does this: i++, which increments i. Then, the code prints the value of i, which is now 1.

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