xxxxxxxxxx
// Example use-case of a foreach loop
const colors = ['red', 'green', 'blue'];
colors.forEach(color => {
if (color === 'green') {
return; // exiting the loop when color is 'green'
}
console.log(color);
});
xxxxxxxxxx
const array = [1, 2, 3, 4, 5];
for (let i = 0; i < array.length; i++) {
if (array[i] === 3) {
break; // Exit the loop if the value is 3
}
console.log(array[i]);
}
xxxxxxxxxx
['a', 'b', 'c'].every(function(element, index) {
// Do your thing, then:
if (you_want_to_break) return false
else return true
})