The Difference Between For Loops and While Loops in JavaScript | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

The Difference Between For Loops and While Loops in JavaScript

As I know and learned we use for loop when we already know how many times we want to loop through something And we use while loop because we don't know how many times we want to loop through something look at these examples for (let i=0; i < 7; i++) { console.log('Hello World'); } we have a variable equal to 0 and this loop helps us to print "Hello World" 7 times so here we know we want "Hello World" to be printed 7 times But in while loop look at the example below let x = 0; while (x < 4) { x++; console.log ("Hello World") } Do you see it? There was no difference between these two loops In both of them, we know exactly how many times we want something to be repeated Even in while loop we know we want "Hello World" to be printed 4 times so how should I know which one is better and where and when? can someone clarify it for me? I'm so confused I've seen resources in Javascript.info, solo learn, Mozilla website and youtube and many other websites but I can't understand and see the difference between these two loops please and please I beg you guys someone explain

15th Aug 2020, 3:28 PM
coal
coal - avatar
5 Answers
+ 5
In the chosen case, there is no difference, but that's only an example to show how the while loop works in general. Imagine a mini program for example where a list of vocabulary is presented to you again and again. You have to click yes, if you know the word, or no if not. If the word is known, it will be erased from the list. The program is supposed to stop when all words are erased. Since you can't foresee how many attempts it will be altogether, you'd use a while loop. while(vocabulary.length>0) { ... }
15th Aug 2020, 3:36 PM
HonFu
HonFu - avatar
+ 2
Ore, you're right, in languages borrowing the syntax from C you can use them equivalently. Personally, if I had a case where one condition decides over the matter, I'd use while. And if I see that I need to iterate anyway, using some 'cremented' variable, I'd pick for.
15th Aug 2020, 4:06 PM
HonFu
HonFu - avatar
+ 1
HonFu Actually that can also be done with a for loop using elision. for(; vocabulary.length>0;) { ... } It boils down to preference, I guess.
15th Aug 2020, 3:49 PM
Ore
Ore - avatar
0
There is no difference. It is just syntax.
15th Aug 2020, 3:30 PM
Ore
Ore - avatar
0
HonFu I agree. However, there are times when I would prefer `for` to `while` even if I have just a condition and no incremental phase. for loops are powerful because they can be initialized. for (app.init(); app.dict; ) { ... } is easier than app.init(); while(app.dict) { } Once again, it is just preference and `while` is best when there is neither initialization nor incremenal phase. Languages like Python don't give you a choice though.
15th Aug 2020, 4:11 PM
Ore
Ore - avatar