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

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

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 Report

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