Between while loop and for loop which one is more faster In JavaScript | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Between while loop and for loop which one is more faster In JavaScript

while vs for loop

24th May 2017, 6:29 AM
diyala
diyala - avatar
2 Answers
+ 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"; }
24th May 2017, 6:36 AM
Dev
Dev - avatar
+ 3
Seems "for" loop is faster. You can check by using console.time(); //start time console.timeEnd(); //end time
24th May 2017, 9:16 AM
Wisdom Abioye
Wisdom Abioye - avatar