Weird looking Javascript for loop -


i have never seen javascript loop such for( ; i-- ; ), used in code:

uid: function (len) {     var str = '';     var src = 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789';     var src_len = src.length;     var = len;      (; i--;) {         str += src.charat(this.ran_no(0, src_len - 1));     }      return str; } 

i understand behavior, if share insights type of loop.

this syntax of for-loop construction:

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

in case for (; i--;) {:

  • no variables initialized, because var = len; inintialized earlier, it's not needed.
  • condition part truthy until i becomes 0 loop terminate. i-- executed on before each iteration, , due -- operator become 0, it's falsy, , loop stop.
  • since i decremented in condition part of loop, final-expression not needed too. way put it: since i not used inside loop, not matter whether decrement before each loop iteration or after each loop iteration.

that being said, it's better avoid writing loops above, it's pretty confusing , hard read. prefer traditional for-loops notation.


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -