for (var i = 0; i < 5; ++i) { alert(i); // 0,1,2,3,4 } for (var i = 0; i < 5; i++) { alert(i); // 0,1,2,3,4 } https://jsfiddle.net/z0ugbbwj/
I did not see the difference.
for (var i = 0; i < 5; ++i) { alert(i); // 0,1,2,3,4 } for (var i = 0; i < 5; i++) { alert(i); // 0,1,2,3,4 } https://jsfiddle.net/z0ugbbwj/
I did not see the difference.
In the given example there is no difference. Learn more about the legend that ++i faster.
It is rumored that to execute ++i value in memory increases and then returns. While for i++ , the value in the created temporary variable is first remembered, then the value of the main variable increases and the value of the temporary variable is returned - thus the “costs” of creating a temporary variable increase.
Performance test fails to look because of a temporary jamb on their side.
In this case, there is no difference, since this expression is executed at the end of the iteration and can be rewritten in this way.
for (var i = 0; i < 5;) { console.log(i); // 0,1,2,3,4 ++i; } for (var i = 0; i < 5;) { console.log(i); // 0,1,2,3,4 i++; } Since this expression is executed separately, and its return value is not used - there is no difference how to change the value of a variable, absolutely similar options:
i += 1; i = i+1; i = inc(i); ... The difference will appear when using the increment in the second block:
for (var i = 0; ++i < 5;) { console.log(i); // 0,1,2,3,4 } for (var i = 0; i++ < 5;) { console.log(i); // 0,1,2,3,4 } The difference is that ++i increments and then returns the value. i++ returns the value and then increments.
Source: https://ru.stackoverflow.com/questions/540829/
All Articles