Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

227
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!