4 sentences to understand the addition and subtraction in js

in conclusion:

  1. Pre-addition: ++ a == a + = 1 == a = a + 1
  2. Add after adding: a++, keep itself unchanged, let the following element with the same name perform +1 operation.
  3. For a, whether it is pre-addition or post-addition, it is the number of operators plus the initial value. There are x operators that are x+initial value
  4. The principle of subtraction is the same as addition

example

  1. Plus plus
var a = 1;
b = ++a + ++a +3 + ++a;
console.log(a);//a等于4
console.log(b);//b等于12

Analysis of calculation process
Insert picture description here2. Post-addition

var a = 1;
var b = a++ + a++ +3 + a++;
console.log(b)//b等于9

Operational process analysis
Insert picture description here3. Pre-addition and post-addition mixing

var a = 1;
var b = a++ + ++a + ++a -2 + a++ +1 + a++ + ++a ;
console.log(b);//b等于23


Insert picture description hereThe principle of subtraction and subtraction is the same as that of addition and addition in the calculation process .

If there is an error, please feel free to correct it; if you have any questions, please leave a message for discussion.

Guess you like

Origin blog.csdn.net/xiaozuo144/article/details/109679597