let i = 1; i = i++ - --i + ++i - i--; console.log(i);
The result shows 0 how is it working with i++ as 1, –i as 1, ++i as 2 and i– as 2.
As I understand i++ should be 1, –i should be 0, ++i should be 1 and i– should be 1.
As post increment or decrement should alter later, pre-increment or decrement works instant.
Please clarify what am I doing wrong in it.
3
Answers
Let’s take it step by step:
i++ returns the value 1 as you say, but it also increments the variable i.
so i now has the value 2.
–i takes 1 off i and returns the value of i, in this case therefore 1.
[So far we have i++ – –i having the value 0 therefore.]
Now ++i increments i which becomes 2 and returns that value
[So far therefore we have 0 – 2]
Finally we have i– which returns the value of i which is 2 (and then takes 1 off it, but that isn’t seen as we then update the lhs (the i variable) with -2 + 2 which is 0.
The
++
operator is overloaded for two types of operands:number
andBigInt
. It first coerces the operand to a numeric value and tests the type of it. It performs BigInt increment if the operand becomes a BigInt; otherwise, it performs number increment.If used postfix, with operator after operand (for example, x++), the increment operator increments and returns the value before incrementing.
If used prefix, with operator before operand (for example, ++x), the increment operator increments and returns the value after incrementing.
The increment operator can only be applied on operands that are references (variables and object properties; i.e. valid assignment targets).
++x
itself evaluates to a value, not a reference, so you cannot chain multiple increment operators together.The
--
operator is also overloaded for two types of operands:number
andBigInt
. It first coerces the operand to a numeric value and tests the type of it. It performs BigInt decrement if the operand becomes a BigInt; otherwise, it performs number decrement.If used postfix
, with operator after operand (for example, x–), the decrement operator decrements and returns the value before decrementing.If used prefix
, with operator before operand (for example, –x), the decrement operator decrements and returns the value after decrementing.The decrement operator can only be applied on operands that are references (variables and object properties; i.e. valid assignment targets).
--x
itself evaluates to a value, not a reference, so you cannot chain multiple decrement operators together.Lets open [i = i++ – –i + ++i – i–;]:
stage-1 => i++ => i=i+1
value of i is 1 (as i++ make post-increment i.e increment value after assigning it)
stage-1 = 1
Now for stage-2 value of i has been incremented so, i = 2 now because of previous post-increment operator,
stage-2 => –i => i=i-1
value of i is 2, but it become 1 because of pre-decrement operator,
stage-2 = 1
stage-3 => ++i => i=i+1
value of i is 1,but it become 2 because of pre-increment operator,
stage-3 = 2
stage-4 => i– => i=i-1
value of i is 2 but it remains 2 because of post-decrement operator not yet applied,
stage-4 = 2
Important Point: Post Decrement Operator not yet applied.
Now let calculate,
stage-1 – stage-2 + stage-3 – stage-4
1 – 1 + 2 – 2 = 0
The value got stored in i before the last post-decrement operation. So, we got 0 in i.
Just like if we do:
We get 0.