Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Weird looking Javascript for loop

Tags:

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.

like image 779
Dejan Toteff Avatar asked Mar 28 '15 11:03

Dejan Toteff


People also ask

What does i ++ mean in JavaScript 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)

Which is better forEach or for loop JavaScript?

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.

What are the different kinds of loops 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

How to loop over an array in JavaScript?

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.

How to loop through the properties of an object in JavaScript?

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.

What is the correct syntax for a for loop?

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.


1 Answers

This is a syntax of the for-loop construction:

for ([initialization]; [condition]; [final-expression])
     statement

In your case for (; i--;) {:

  • no variables are initialized, because var i = len; inintialized earlier, so it's not needed.
  • condition part will be truthy until 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.
  • since 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.

like image 161
dfsq Avatar answered Sep 19 '22 04:09

dfsq