What is the best way to do loops in JavaScript

前端 未结 9 1887
温柔的废话
温柔的废话 2020-12-13 00:35

I have stumbled into several methods of looping in JavaScript, what I like the most is:

for(var i = 0; i < a.length; i++){
    var element = a[i];
}
         


        
9条回答
  •  自闭症患者
    2020-12-13 01:14

    I know I'm late to the party, but I use reverse loops for loops that don't depend on the order.

    Very similar to @Mr. Muskrat's - but simplifying the test:

    var i = a.length, element = null;
    while (i--) {
      element = a[i];
    }
    

提交回复
热议问题