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

236
Visualizações
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 Respostas
Responde à pergunta

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 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