+ 2

x=1 y=x + x++ alert(y);

Why the result is 2 ??

27th Jan 2018, 1:39 PM
Demi Louka
Demi Louka - avatar
6 Answers
+ 9
When you do (x++), the value of x is returned before x is incremented. As a result, y = 2, x is incremented by 1, and then the value of y is printed.
27th Jan 2018, 1:43 PM
Hatsy Rei
Hatsy Rei - avatar
+ 8
@Demi yeah if x++ then you can say that y=x+x; for more information go through this post it will help you https://www.sololearn.com/Discuss/407846/?ref=app
27th Jan 2018, 1:50 PM
GAWEN STEASY
GAWEN STEASY - avatar
+ 7
x=1 y=x+x++ //let's break this line y=1+1 //because x++ uses the value first then increments later. so... y=1+1 y=2
27th Jan 2018, 1:44 PM
᠌᠌Code X
᠌᠌Code X - avatar
+ 3
x is post incremented. That means it first use the current value and then it is incremented. Here y=1+1=2. so alert(y) //outputs 2 Try alert(x) // outputs 2 Try alert(x+y) //output will be 4.
27th Jan 2018, 1:50 PM
Sunil Thakali
Sunil Thakali - avatar
+ 1
so we can say that y=x+x++ is the same as y=x+x because the result in the end is the 2 in both
27th Jan 2018, 1:48 PM
Demi Louka
Demi Louka - avatar
+ 1
ok thank you all
27th Jan 2018, 1:53 PM
Demi Louka
Demi Louka - avatar