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));
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
+ 9
return bar(b++) calls "bar(6)" function.
Which returns (a + b) or (5 + 6)..
+ 5
Thanks Aravind Shetty, for that explanation, it was the "8" in the "foo(8)" that was putting me off
+ 2
WTF this((((( OMG