+ 2
Javascript Problem with function
function y(x){ x= x+1; return x++; } console.log(y(2)); Why it's returning 3? X= 2 Then, x= x+1 //x=3 Then, x++; // x = 4? Help me please
4 Answers
+ 4
In your function you type 5 but in comments 1, I think you make typo and meant x = x + 1
Reason why it return 3 not 4 is because of how increment work.
When you type x++ it will add 1 to x but after this line of code end.
So when you return, value is 3, but after return it will be 4 (this will never run because return exit from function)
Check my code to understand better
https://code.sololearn.com/W56Ig5vNOH7R/?ref=app
+ 2
PanicS Thanks now I understand.đŽ
+ 2
Sachin
x++ post increment first assign then increment by 1
++x pre increment first increment by 1 then assign
So if there is ++x then x = 4
+ 2
A͢J Thanks