+ 1
Between while loop and for loop which one is more faster In JavaScript
while vs for loop
2 Antworten
+ 17
You can always use either of the loops. But, the most common usage of forloops is when the number of iterations are known, and the while loop having uncertain number of iterations or would depend upon a particular condition. Both are good at their own practical works. For Eg.,
var arr = ["a", "b", "c"];
for (let i=0; i<arr.length; i++) { // will loop 3 times (length of arr = 3) // do something }
var x = "Hello";
while (x != "World") { // do something if (some_condition) { // do something } else x = "World"; }
+ 3
Seems "for" loop is faster.
You can check by using
console.time(); //start time
console.timeEnd(); //end time