user1360809
user1360809

Reputation: 745

Do the 'continue' and 'break' statements work with a for...in?

The title says it all, to confirm the current question in a more formal manner:

Do the 'continue' and 'break' statements work with a for...in loop in JavaScript?

I have tried using a label with my for...in loop but it breaks the application outright...if I cannot do this is there anything else I can do?

I need this functionality because I am error checking and if an error is found regarding the current item I want to report this error then continue to the next item. Solutions using pure JavaScript preferred if possible.

Thanks in advance for any help!

Upvotes: 1

Views: 159

Answers (2)

arthur.li
arthur.li

Reputation: 1

Absolute Yes. Very easy to try on Firebug.

Upvotes: 0

Benjamin Gruenbaum
Benjamin Gruenbaum

Reputation: 276306

Yes. They do.

for(var i in window){
   break;
   console.log(i);
}

Prints nothing.

If we check the language specification we can see that:

Break is evaluated as: Return (break, empty, empty).

Which terminates the current block.

More generally - it'll work with a block:

{
    break; // this works, no alert
    alert("HI");   
}

while(true){
    break; // this works, no alert
    alert("HI");   
}

for(var fake in window){
    break; // this works, no alert
    alert("HI");   
}

Upvotes: 2

Related Questions