This is what I usually write for loops.
for(var i = 0; i < 20 ; i++){ .... }But I think some people write this
for (var i = 0; i < 20 ; ++i) {..}What impact will this have on the for loop?
1. It seems that there is no difference in for...
But when used in assignment, there is a difference...
var a = 1;var b = a++;var c = 1;var d = ++c;console.log(b,d); // 1, 2
a++ is to apply a first, then add it yourself
++a is to add it first, and then apply a
2. It is probably a habit brought by the C/C++ era. Legend has it that ++i is faster than i++, but in fact, the performance of these two is similar in js.
As for the specific details, the specification is written as follows:
For the situation in front -
UnaryExpression : ++ UnaryExpressionLet expr be the result of evaluating UnaryExpression.Let oldValue be ToNumber(GetValue(expr)).ReturnIfAbrupt(oldValue).Let newValue be the result of adding the value 1 to oldValue, using the same rules as for the + operator (see 12.7.5).Let status be PutValue(expr, newValue).Return IfAbrupt(status).Return newValue.
A rough translation:
1. Let expr equal the result of the current expression (that is the part after ++)
2. Let oldValue equal to the result of expr converting to a number
3. Use the same rules as the "+" operator, let newValue = oldValue + 1
4. Assign newValue to expr
5. The entire operation returns newValue
For the situation behind it -
PostfixExpression: LeftHandSideExpression ++
1.Let lhs be the result of evaluating LeftHandSideExpression.
2.Let oldValue be ToNumber(GetValue(lhs)).
3. ReturnIfAbrupt(oldValue).
4.Let newValue be the result of adding the value 1 to oldValue, using the same rules as for the + operator (see 12.7.5).
5.Let status be PutValue(lhs, newValue).
6. ReturnIfAbrupt(status).
7. Return oldValue.
A rough translation -
1. Let lhs equal the result of the current expression (the previous part of ++)
2. Let oldValue equal to lhs to convert the result of number
3. Use the same rules as the "+" operator, let newValue = oldValue + 1
4. Assign newValue to lhs
5. The entire operation returns oldValue
It can be seen that the difference between the two is only the last step. Whether the entire operation returns after adding one or before adding one.
For the for statement:
for(var i = 0; i < n; i++)
The last column is just self-added and does not assign this value to anyone, so from this point of view, it is the same for ++ symbols to be placed in front or after.
3. In fact, there is not much difference. But if you want to pursue that little bit of efficiency, ++i will indeed be more efficient.
It is indeed from the std standard library of c++. When i is an iterator of a std container, i is not a number, i++ will generate an additional assignment operation, so ++i has higher performance. Therefore, people who are used to C++ like to use ++i to save money when i is not a numerical value.
The above is the difference between i++ and ++i in JS for loop introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!