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

105
Views
javascript function output problrm

The output of console.log(in1()) should be 11 but it's 10 why?

function outer() {
  let i = 10;
  return function inner() {
    return i++;
  }
}

var in1 = outer();
console.log(in1());

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Quoting https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Increment here:

If used postfix, with operator after operand (for example, x++), the increment operator increments and returns the value before incrementing.

Therefore, the postfix (that is, x++; postfix because it appears after) operator only gives the existing value before doing anything. That's why, when called, gives what exists, not what's the result of the addition.

Because of this, use the prefix operator instead, ++x, to get the value while still incrementing the variable.

The same applies to the decrement operator.

about 4 years ago · Juan Pablo Isaza Report

0

It's because using i++ returns i first, then adds 1 to i. If you call in1() the second time, you will see 11, 12 and so on. Try using ++i instead.

about 4 years ago · Juan Pablo Isaza Report

0

When using the postfix syntax i++ the value is returned before incrementing. Use the prefix syntax instead ++i.

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!