-1

Is there anything I am missing about forEach loop this gives an error

VM847:5 Uncaught SyntaxError: Illegal break statement at Array.forEach () at :2:9

And this is working fine for normal for loop.

var myArray = [{name:'John'}, {name:'Doe'}, {name:'Mice'}]
myArray.forEach(function(item, i){     
    if(item.name == 'Doe'){
      console.log(i);
      break;
    }
});
Ankit Pandey
  • 608
  • 4
  • 17

1 Answers1

1

You cannot break a forEach loop, if you want it to stop you would have to throw an exception

marvel308
  • 10,288
  • 1
  • 21
  • 32