I learned what the different between post-increment and pre-increment was, but still surprised by the below code:
let array = [1];
array = [array[0] ];
console.log(array);
I expected array[0]
would be returned on the right hand first, then array
would be assigned as [1]
. After that, array[0]
would be executed, and array
should be [2]
.
Thanks for any explanation.
CodePudding user response:
let array = [1];
let item = array[0] ; // item = 1
array = [item]; // array = [1]
CodePudding user response:
As we see in C
int x= 2;
cout<< x ;
Just like in C , the control first sets/prints the value and then increment it. It is the same case here.
let array = [1];
array = [array[0] ];
console.log(array);
However, You can use the following below code to see difference
let array = [1]; //array[0]=1
array = [ array[0]]; //array[0]=2
console.log(array);