Strange syntax error on 12/13 (for loops and arrays)


#1

There is something weird with my syntax. When I type:

var names = [ "Benjamin", "Luke", "Bob", "Veda", "Alejandro"];
for (var i = 0 , i < names.length , i++) {
console.log("I know someone called"+ names[i]);
}

and click the "Save & Submit Code" button, it says : "SyntaxError: Unexpected token <".

but when I type:

var names = [ "Benjamin", "Luke", "Bob", "Veda", "Alejandro"];
for ( i = 0 , i < names.length , i++) {
console.log("I know someone called"+ names[i]);
}

basically the same thing without the var, it says: "SyntaxError: Unexpected token )".

Why is that?? And what can I do to solve this problem and move on to the next lesson?


#2

There is a comma after i = 0. Because of that, the interpreter is still waiting for normal expression and it get's just the logical one. You can find more about the comma operator here -> MDN: Comma operator.

To fix this problem you just have to change commas in the for loop to semicolons.