admin管理员组文章数量:1323714
Here's my code:
var x = 3;
var y = x++;
y += 1;
Output: y = 4
I know the puter is right, but I'm not sure why the puter is right. y gets assigned the value 3 from x, then it increments it to 4 in line 2. So, the output from line 3 should be 5, correct?
From what I've read, y gets assigned the value of x before the increment happens, but when it does happen, why does the value of y not change?
Here's my code:
var x = 3;
var y = x++;
y += 1;
Output: y = 4
I know the puter is right, but I'm not sure why the puter is right. y gets assigned the value 3 from x, then it increments it to 4 in line 2. So, the output from line 3 should be 5, correct?
From what I've read, y gets assigned the value of x before the increment happens, but when it does happen, why does the value of y not change?
Share Improve this question edited Jan 10, 2019 at 19:09 SeriousBurn asked Jan 10, 2019 at 18:39 SeriousBurnSeriousBurn 31 silver badge4 bronze badges 2-
2
Assignment of a value from one variable to another in JavaScript involves a copy of the value. In
y = x++;
no permanent relationship betweenx
andy
is established; it's a one-time copy. – Pointy Commented Jan 10, 2019 at 18:42 -
2
"y gets assigned the value of x" That's the crucial point.
x++
is equivalent tox = x + 1
. Soy = x++
is the same asy = x; x = x + 1;
.x
is assigned a new value, but that has no effect on the value ofy
. – Felix Kling Commented Jan 10, 2019 at 18:45
3 Answers
Reset to default 4In your assignment y = x++;
the value of y is first assigned to x and then the variable x gets incremented by 1. By performing this operation y bees 3 and x is 4. Then after running y +=1 puter will calculate 3+1 = 4
If you're expecting y to be 5 you should do y = ++x;
. By doing this x will first get incremented by 1 and then assigned to y so we will have y = 4 and x = 4 following the y += 1 (4+1=5)
There is a difference between pre-increment (++x
) and post-increment (x++
).
A pre-increment operator is used to increment the value of a variable before using it in a expression. In the pre-increment, value is first incremented and then used inside the expression. Let's say we have:
a = ++x;
Here, if the value of ‘x’ is 10 then value of ‘a’ will be 11 because the value of ‘x’ gets modified before using it in the expression. This is equivalent with:
x = x + 1;
a = x;
A post-increment operator is used to increment the value of variable after executing expression pletely in which post increment is used. In the Post-Increment, value is first used in a expression and then incremented. Let's say we have:
a = x++;
Here, suppose the value of ‘x’ is 10 then value of variable ‘a’ will be 10 because old value of ‘x’ is used. This is equivalent with:
a = x;
x = x + 1;
You can read more on the interned about this (for example, here or here).
Cheers!
// Post-increment example
console.log("post-increment examples");
let x = 10;
a = x++;
console.log(x, a);
x = 10;
a = x;
x = x + 1;
console.log(x, a);
// Pre-increment example
console.log("pre-increment examples");
x = 10;
a = ++x;
console.log(x, a);
x = 10;
x = x + 1;
a = x;
console.log(x, a);
x++
return the value and then add 1. See this:
var x = 3;
var y = x++;// x return 3 and then add 1, y is 3
y += 1;//3 + 1 = 4
console.log(y)
console.log(x)//x return 4
本文标签:
版权声明:本文标题:javascript - In an increment, why does the value of the variable get passed instead of the value as well as the increment? - Sta 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742128379a2422046.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论