The Array.forEach() loop is a built-in method in JavaScript that is used to reiterate over any array and execute a function for each element of the array. The main purpose of this method is to perform some operation on each element of the array without having to manually iterate over it using a loop.
The Array.forEach() method is used to execute a provided function once for each element in an array. However, it does not provide a built-in mechanism to break or exit the loop prematurely like the break statement in a traditional for loop.
However, there are several ways to achieve a similar effect and short-circuit the loop based on some conditions.
Approach 1: Array.some(): The Array.some() method can be used to iterate over an array and return true as soon as the condition is met, thereby short-circuiting the loop. If the condition is not met for any element, the loop continues until all elements have been processed.
Syntax:
array.some(callback(element[, index[, array]])[, thisArg])
- array: The array to be iterated over.
- callback: The function to be called for each element in the array, which takes the following arguments:
- element: The current element of the array.
- index (optional): The index of the current element of the array.
- array (optional): The array that some were called upon.
- thisArg (optional): In the callback function the object to which this keyword can refer. The default value is undefined.
- callback: The function to be called for each element in the array, which takes the following arguments:
The some() method returns true if the callback function returns a truthy value for at least one element in the array; otherwise, false.
Example:
Javascript
const array = [2, 4, 8, 10]; let found = false ; array.some( function (element) { if (element === 6) { found = true ; return true ; // Short-circuits the loop } }); console.log( "Output: " , found); // false |
Output:
Output: false
Approach 2: for…of loop: Another way to short-circuit an array loop is to use a for…of loop instead of Array.forEach(). A for…of loop provides a built-in mechanism to break or exit the loop prematurely using the break statement.
Syntax:
for (variable of iterable) { // code to be executed }
- variable: A new variable to be assigned to each element in the iterable object.
- iterable: An object that can be iterated over, such as an array, a string, a map, or a set.
Example:
Javascript
const array = [9, 4, 3, 11, 10]; let isFound = false ; for (const element of array) { if (element === 3) { isFound = true ; break ; // short-circuits the loop } } console.log( "Output: " , isFound); // true |
Output:
Output: true
Approach 3: Array.every() Method: Similar to Array.some(), the Array.every() method can also be used to iterate over an array and return false as soon as the condition is not met, thereby short-circuiting the loop. If the condition is met for all elements, the loop continues until all elements have been processed.
Syntax:
array.every(callback(element[, index[, array]])[, thisArg])
- array: The array to be iterated over.
- callback: The function to be called for each element in the array, which takes the following arguments:
- element: Element which is currently being processed.
- index (optional): Index of the element currently being processed.
- array (optional): The array that everyone was called upon.
- thisArg (optional): In the callback function the object to which this keyword can refer. The default value is undefined.
- callback: The function to be called for each element in the array, which takes the following arguments:
The every() method returns true if the callback function returns a truthy value for all elements in the array; otherwise, false.
Example:
Javascript
const array = [11, 12, 13, 14, 51]; let isOdd = true ; array.every( function (el) { if (el % 2 === 0) { isOdd = false ; return false ; // short-circuits the loop } return true ; }); console.log( "Output: " , isOdd); // false |
Output:
Output: false