When to while and when to for


#1

while(4%2===0) {
return "true value";
}
for(i=0; i< 4; i++) {
console.log(i);
};
where am i wrong? it says syntax error


#2

@arccoder77240

Declare your i variable by putting the var keyword in front of the first i.


#3

i put var in front of first i but still not working


#4

Is this code part of a function? Otherwise return is not allowed to be used here.


#5

when i put console.log instead of return it becomes unresponsive


#6

Sure it does, because the condition is true and stays true therefore you're trapped in an infinite loop.


#7

having an unexpected token < error. thanks.

/*
var wl = function() {
var wlv = 1;
while (wlv <=8) {
console.log("WL!");
wlv++;
}
}

var fl = function() {
for (var i = 1, i < 8, i++) {
console.log("FL [i]");
}
} */


#8

you need to use ; instead of , here.