I have never seen a JavaScript loop such as this for( ; i-- ; )
, used in the code:
uid: function (len) { var str = ''; var src = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; var src_len = src.length; var i = len; for (; i--;) { str += src.charAt(this.ran_no(0, src_len - 1)); } return str; }
I understand the behavior, but I would like it if somebody could share some insights about this type of for loop.
++ is the increment operator.. for ex i++ means i=i+1 for(int i=0;i<10;i++) { System. out. printline(i); } In the following example first of all the intial value of i is 0 so 0<10 it comes inside the loop and print the value of i again the value of i is incremented to 1(i=i+1)
forEach is almost the same as for or for..of , only slower. There's not much performance difference between the two loops, and you can use whatever better fit's the algorithm. Unlike in AssemblyScript, micro-optimizations of the for loop don't make sense for arrays in JavaScript.
JavaScript supports different kinds of loops: for - loops through a block of code a number of times for/in - loops through the properties of an object while - loops through a block of code while a specified condition is true do/while - also loops through a block of code while a specified condition is true
The JavaScript for in statement can also loop over the properties of an Array: Do not use for in over an Array if the index order is important. The index order is implementation-dependent, and array values may not be accessed in the order you expect. It is better to use a for loop, a for of loop, or Array.forEach () when the order is important.
The JavaScript for in statement loops through the properties of an Object: The JavaScript for in statement can also loop over the properties of an Array: Do not use for in over an Array if the index order is important. The index order is implementation-dependent, and array values may not be accessed in the order you expect.
The For Loop. The for loop has the following syntax: for ( statement 1; statement 2; statement 3) {. // code block to be executed. }. Statement 1 is executed (one time) before the execution of the code block. Statement 2 defines the condition for executing the code block.
This is a syntax of the for-loop
construction:
for ([initialization]; [condition]; [final-expression])
statement
In your case for (; i--;) {
:
var i = len;
inintialized earlier, so it's not needed.i
becomes 0
then loop will terminate. i--
is executed on before each iteration, and due to --
operator it will eventually become 0
, so it's falsy, and loop will stop. i
is decremented in condition part of the loop, final-expression
is not needed too. Another way to put it: since i
is not used inside the loop, it does not matter whether we decrement it before each loop iteration or after each loop iteration.That being said, it's better to avoid writing loops like above, as it's pretty confusing and hard to read. Prefer traditional for-loops notation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With