Skip to content

Instantly share code, notes, and snippets.

@tangoabcdelta
Created January 18, 2021 04:27
Show Gist options
  • Save tangoabcdelta/22937e0320f8e966a6a27b07f2847637 to your computer and use it in GitHub Desktop.
Save tangoabcdelta/22937e0320f8e966a6a27b07f2847637 to your computer and use it in GitHub Desktop.
break out of a forEach loop in JavaScript
// just truncate the array's length:
const myArray = [1, 2, 3];
myArray.forEach(item => {
// ... do some stuff
if(someConditionIsMet) {
// Break out of the loop by truncating array
myArray.length = 0;
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment