8. When to While and when to For



I went freestyle here and I was wondering if anyone can spot any issues in my code.

var i = 1;
var loop = function (){
while (i < 2) {
console.log ("To be, or not to be");
count += 1;
} }
loop ();

for (j = i +1, j < 3, j++ ) {
console.log ("Not to be!! NOT TO BE!");

I'm getting this as error
SyntaxError: Unexpected token )
(Could someone explain to me what does that error mean? I've faced it plenty of times and i can't quite understand what it means since I've got all the brackets closed.)

Thank you loads :slightly_smiling:


The problem is in you for loop.

In the parenthesis of the for loop you should put three expressions:

  1. Expression that will be executed at the beginning of the for loop.
  2. A condition that will be checked before starting every iteration.
  3. Expressions that will be executed after every iteration.

In your code there is only one expression -> j = i +1, j < 3, j++ and that is why next ) is unexpected. Reason is simple, you should separate expressions with the semicolon :slightly_smiling:

for (j = i + 1; j < 3; j++) {
    console.log("Not to be!! NOT TO BE!");


hi @factoradic

Thank you!
Wow i didn't realise that a semicolon can make such a huge difference!

Thank you loads again! :slightly_smiling:


This is beauty (or horror if you prefer) of programming :slightly_smiling:

You're very welcome!