Javascript Increment and Decrement operator

65 Views Asked by At

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

There are 3 best solutions below

0
A Haworth On

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.

0
Muneeb Khan On
let i = 1; i = i++ - --i + ++i - i--; console.log(i);

Lets open [i = i++ - --i + ++i - i--;]:

i = i=i+1 - i=i-1 + i=i+1 - i=i-1

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:

let i = 0; i = i--; console.log(i); 

We get 0.

0
GauRang Omar On

If we breakdown the expression step by step:

  • i++ - This is a post-increment operation. It means the current value of i (which is 1) is used in the expression, and then i is incremented by 1. So, i++ evaluates to 1, and after this operation, i becomes 2.

  • --i - This is a pre-decrement operation. It means i is decremented by 1, and then the new value (which is 1) is used in the expression. So, --i evaluates to 1.

  • ++i - This is a pre-increment operation. It means i is incremented by 1, and then the new value (which is 2) is used in the expression. So, ++i evaluates to 2.

  • i-- - This is a post-decrement operation. It means the current value of i (which is 2) is used in the expression, and then i is decremented by 1. So, i-- evaluates to 2, and after this operation, i becomes 1.

Now let's substitute these values into the original expression: 1 - 1 + 2 - 2

Now, when we perform the arithmetic operations the console.log(i) would output 0.