Débora
Débora

Reputation: 5952

Javascript continue on forEach

Is it impossible to use 'continue' with forEach ? Following gives errors.

var numbers = [4, 9, 16, 25];
function myFunction() {
  numbers.forEach(function(val){ 
   if(index==2){
      continue;
    }
    alert('val: '+val);
  }); 
}

Is there any other work around to use continue?

Upvotes: 2

Views: 5784

Answers (2)

Akshey Bhat
Akshey Bhat

Reputation: 8545

var numbers = [4, 9, 16, 25];
function myFunction() {
  numbers.forEach(function(val, index){ 
    if(index!=2)
      alert('val: '+val);
}); 

Upvotes: -1

Quentin
Quentin

Reputation: 943215

Is it impossible to use 'continue' with forEach ?

Yes.

Is there any other work around to use continue?

Use return to exit a function immediately.

Upvotes: 11

Related Questions