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

106
Views
Returned value not expected - closures

I don't understand why it returns 0, it is supposed to increment by 1, is there something I don't know about closures?

 let i = 0;
    const getRenderValue = function() {

      return i++;
    };
    const Component = function() {
      let hello = getRenderValue();
      console.log(hello); // return 0, expected value 1
    };
    Component();
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Your closure does increment the value of i by one. If you did console.log(i); instead of console.log(hello);, you'd be seeing 1 on screen as expected.

Your closure however returns the value of i prior to the increment as it returns i++: It is a property of the postfix increment i++ that it returns i before and not after the increment. Use the prefix increment ++i if you want to return the changed value.

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!