Created
January 18, 2021 04:27
-
-
Save tangoabcdelta/22937e0320f8e966a6a27b07f2847637 to your computer and use it in GitHub Desktop.
break out of a forEach loop in JavaScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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