最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Explain, please, these ++ and -- operations - Stack Overflow

programmeradmin2浏览0评论

Why this code outputs 3, not 2?

var i = 1; 
i = ++i + --i; 
console.log(i);

I expected:

++i // i == 2
--i // i == 1
i = 1 + 1 // i == 2

Where I made mistake?

Why this code outputs 3, not 2?

var i = 1; 
i = ++i + --i; 
console.log(i);

I expected:

++i // i == 2
--i // i == 1
i = 1 + 1 // i == 2

Where I made mistake?

Share Improve this question asked Aug 2, 2011 at 16:35 Sergey MetlovSergey Metlov 26.3k28 gold badges96 silver badges154 bronze badges 4
  • 1 search stack overflow for ++ and -- postfix and prefix operators – Aaron Yodaiken Commented Aug 2, 2011 at 16:36
  • ++i = 2, then i = 2, --i = 1. Therefore 2+1=3 because javascript statements are evaluated from left to right. – Joe Commented Aug 2, 2011 at 16:38
  • 2 By your own logic, i should be equal to 3. – kinakuta Commented Aug 2, 2011 at 16:38
  • 3 I don't get it. You are stating yourself, that ++i evaluates to 2 and --i evaluates to 1, if you add up the two it's 3 - why would it be 2? – M. Cypher Commented Aug 2, 2011 at 16:39
Add a ment  | 

8 Answers 8

Reset to default 10

The changes occur in this order:

  1. Increment i (to 2)
  2. Take i for the left hand side of the addition (2)
  3. Decrement i (to 1)
  4. Take i for the right hand side of the addition (1)
  5. Perform the addition and assign to i (3)

… and seeing you attempt to do this gives me some insight in to why JSLint doesn't like ++ and --.

Look at it this way

x = (something)
x = (++i) + (something)
x = (2) + (something)
x = (2) + (--i)
x = (2) + (1)

The terms are evaluated from left to right, once the first ++i is evaluated it won't be re-evaluated when you change its value with --i.

Your second line is adding 2 + 1.

In order, the interpreter would execute:

++i  // i == 2
+
--i  // i == 1
i = 2 + 1

++i equals 2, `--i' equals 1. 2 + 1 = 3.

You're a little off on your order of operations. Here's how it goes:

  1. i is incremented by 1 (++i) resulting in a value of 2. This is stored in i.
  2. That value of two is then added to the value of (--i) which is 1. 2 + 1 = 3

Because when you use ++i the value of i is incremented and then returned. However, if you use i++, the value of i is returned and then incremented. Reference

++$a   Increments $a by one, then returns $a.
$a++   Returns $a, then increments $a by one.
--$a   Decrements $a by one, then returns $a.
$a--   Returns $a, then decrements $a by one.

Because you're expecting this code to work as if this is a reference object and the values aren't collected until the unary operations are plete. But in most languages an expression is evaluated first, so i returns the value of i, not i itself.

If you had ++(--i) then you'd be right.

In short, don't do this.

The result of that operation isn't defined the same in every language/piler/interpreter. So while it results in 3 in JavaScript, it may result in 2 elsewhere.

发布评论

评论列表(0)

  1. 暂无评论