JavaScript for-loop alternative: repeat(n, function(i) { … });

前端 未结 5 1006
臣服心动
臣服心动 2021-02-09 22:05

This is the regular for-loop:

for (var i = 0; i < n; i++) { ... }

It is used to iterate over arrays, but also to just repeat some process

5条回答
  •  忘掉有多难
    2021-02-09 22:39

    it's an interesting thought, but if you dislike the syntax for the loop, you could always do a different type of loop:

    var i = arr.length; 
    while (i--) {
        // do stuff
    }
    

    the reverse while loop is generally faster than a for loop as well.

提交回复
热议问题