JavaScript "I would like to understand why the output is 7" | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

JavaScript "I would like to understand why the output is 7"

function calc(j,p) { j *= p++; return j+p; j-= p; } alert(calc(2,2)); output is 7

12th Mar 2017, 7:26 PM
Martin Daigneault
Martin Daigneault - avatar
2 Answers
+ 17
First of all.. j = j * p++... j = j * p, which makes j = 4.. But because ++ is in the end is incremented after assignment so p is 3.. Then the function returns 4 + 3 which is 7.. Everything after return is ignored...
12th Mar 2017, 7:27 PM
Gami
Gami - avatar
+ 1
thanks
12th Mar 2017, 7:33 PM
Martin Daigneault
Martin Daigneault - avatar