Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
99
rated 0 times [  102] [ 3]  / answers: 1 / hits: 60854  / 12 Years ago, fri, february 22, 2013, 12:00:00

I have a short javascript code where I need to skip to next in the for loop....see below:



var y = new Array ('1', '2', '3', '4');
for (var x in y) {
callFunctionOne(y[x]);
while (condition){
condition = callFunctionTwo(y[x]);
//now want to move to the next item so
// invoke callFunctionTwo() again...
}
}


Wanted to keep it simple so syntax may be error free.


More From » for-loop

 Answers
9

Don't iterate over arrays using for...in. That syntax is for iterating over the properties of an object, which isn't what you're after.



As for your actual question, you can use the continue:



var y = [1, 2, 3, 4];

for (var i = 0; i < y.length; i++) {
if (y[i] == 2) {
continue;
}

console.log(y[i]);
}


This will print:



1
3
4





Actually, it looks like you want to break out of the while loop. You can use break for that:



while (condition){
condition = callFunctionTwo(y[x]);
break;
}


Take a look at do...while loops as well.


[#80057] Thursday, February 21, 2013, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
daja

Total Points: 407
Total Questions: 103
Total Answers: 103

Location: Ghana
Member since Sun, Mar 27, 2022
2 Years ago
daja questions
Tue, Dec 21, 21, 00:00, 3 Years ago
Thu, Apr 23, 20, 00:00, 4 Years ago
Fri, Sep 6, 19, 00:00, 5 Years ago
Tue, Jul 23, 19, 00:00, 5 Years ago
;