....I still don't understand why the code outputs 11. Please help me explain the reason for its output as 11 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 6

....I still don't understand why the code outputs 11. Please help me explain the reason for its output as 11

function foo(a) { var b = 5; function bar(a) { return b + a; } return bar(b++); } console.log(foo(8));

28th Jun 2019, 5:01 PM
eMBee
eMBee - avatar
4 Answers
+ 4
foo(8) assigns 5 to b bar(b++) = incremented b (=6) + b++(=5++) we know that 6 + 5++ = 11 hence the function returns 11
28th Jun 2019, 5:11 PM
Aravind Shetty
Aravind Shetty - avatar
+ 9
return bar(b++) calls "bar(6)" function. Which returns (a + b) or (5 + 6)..
28th Jun 2019, 5:49 PM
Lawz
Lawz - avatar
+ 5
Thanks Aravind Shetty, for that explanation, it was the "8" in the "foo(8)" that was putting me off
28th Jun 2019, 5:22 PM
eMBee
eMBee - avatar
+ 2
WTF this((((( OMG
28th Jun 2019, 5:32 PM
Azizbek Rasulmetov
Azizbek  Rasulmetov - avatar