Skip to content

Instantly share code, notes, and snippets.

@kobeumut
Created October 4, 2017 10:56
Show Gist options
  • Select an option

  • Save kobeumut/637cabd70d0fc8df845d86ed48a7a851 to your computer and use it in GitHub Desktop.

Select an option

Save kobeumut/637cabd70d0fc8df845d86ed48a7a851 to your computer and use it in GitHub Desktop.
For loop break is problem for foreach in Javascript. There is no built break in for each. You can do throw like that
var exceptionOfBreak = {};
try {
["a", "b", "c"].forEach(function(el) {
console.log(el);
if (el === "b") throw exceptionOfBreak;
});
} catch (e) {
if (e == exceptionOfBreak) console.log("for loop is break");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment