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

228
Vistas
Javascript SetInterval initial increment - why not 0?

Given the following function:

    let ticks = 0;
    let clock = setInterval(() => {
      console.log("tick", ticks++);
      if (ticks == 10) {
        clearInterval(clock);
        console.log("stop.");
      }
    }, 200);

Why is the first thing logged to the console "tick 0" rather than "tick 1", since it is immediately incrementing ticks by 1 (ticks++)? Or, is it the case that JS sees "ticks" first and immediately logs that, then sees the ++ and increments it accordingly?

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

0

ticks++ evaluates to ticks, and after that, adds 1. You probably want ++ticks.

It jumps at you right at the start of the documentation of the ++ operator:

If used postfix, with operator after operand (for example, x++), the increment operator increments and returns the value before incrementing.
If used prefix, with operator before operand (for example, ++x), the increment operator increments and returns the value after incrementing.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Increment#description

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