Ingenious understanding of js increment and decrement operators
Increment and decrement operator: If you need to repeatedly add or subtract 1 to a numeric variable, you can use the increment (++) and decrement (--) operators to complete. In js, increment or decrement can be placed in front of the variable, or you can Put it after the variable, pre-increment (put it at the front), and post-increment (put it at the back). Note: The increment and decrement operator must be used in conjunction with the variable. The pre-increment operator : ++num means self-increment 1 num = num + 1 ("++ is written in front of the variable)
var age = 10;
++age; //类似于age = age + 1
console.log(age);//11
Pre-increment calculation formula : first add 1 and then return value
var p = 10;
console.log(++p + 10);//21
Post-increment operator
var num = 10;
num++; //类似于num = num + 1
console.log(num);//11
Note: Pre-increment and post-increment have the same effect when used alone.
Post-increment calculation formula : return to the original value first, then add 1
var age = 10;
console.log(age++ + 10); // 20
console.log(age); //11
Exercise 1
var a = 10;
++a;
var b = ++a + 2;
console.log(b); //14
Exercise 2
var c = 10;
c++; //单独使用也是自加1 c = 11
var d = c++ + 2; //c++ =11 c=12
console.log(d); //13
Exercise 3
var e = 10;
var f = e++ + ++e; //1. e++=10 e=11 2.++e=12 e=12
console.log(f); //22