JavaScript "self-increment" operators and expressions
The increment operator (+ +) is a unary operator that increments the operand by 1 at a time. The increment operator requires that its operand be a variable, a property in an object, or an element in an array, and that the type of the operand must be numeric.
If the operand type is not a numeric type, the increment operator converts the its first to numeric data and then increments the operation.
The increment operator (the self-increment operator) has two different increments depending on its position relative to the operand.
1. "Pre-increment" operator
Format:
++a
The pre-increment operator first adds 1 to the value of the operand and then participates in the operation of the expression using the new value of the operand.
Example:
var a=10, B;b=++a;
Results: a=11,b=11
2. "Post-increment" operator
Format:
a++
The post-increment operator first participates in the operation of the operand's value, after which the value of the expression is referenced, and then adds 1 to the value of the operand.
Example:
var a=10, B; b=a++;
Results: a=11,b=10
tip: when "+ +" is in front of a (++a), a plus 1 participates in the expression operation, and when "+ +" is behind A (a++), a first participates in the expression operation, and then adds 1.
"+ +" is only meaningful to a, you can think (++a) or (a++) as a whole, but a first plus 1 to participate in the operation, or to participate in the operation and add 1 of the problem.
Case One
var a = 1;
var B = ++a + ++a;
Console.log (b); Result is 5
var a = 1;
var c = a++ + a++;
Console.log (c); Result is 3
var a = 1;
var d = a++ + ++a;
Console.log (d); Result is 4
var a = 1;
var e = ++a + a++;
Console.log (e); Result is 4
Case Two
var a = 1;
var B = ++a + ++a; ==> first a=a+1 get a=2, then 2 + ++a, then a=a+1 get a=3, finally 2+3=5, at this time a value of 3
Console.log (b); Result is 5
var c = a++ + a++; The value of a after the ==> operation is 5
Console.log (c); Result is 7
var d = a++ + ++a; The value of a after the ==> operation is 7
Console.log (d); Result is 12
var e = ++a + a++; The value of a after the ==> operation is 9
Console.log (e); Result is 16
JavaScript "decrement" operator and expression
The decrement operator (the self-decrement operator) has two different ways of decreasing its position relative to the operand, that is, the pre -decrement and post -decrement.
The method of operation above, here is not more verbose.
JavaScript's self-amplification typical operation (error-prone)