The JavaScript every()
method navigate to all array elements and execute a function. This loop exits if function returns false with any array loop and doesn’t check remaining elements. The every() method doesn’t execute without values arrays. Also it doesn’t change the original array.
Advertisement
Syntax:
The syntax of JavaScript every() method is as below.
array.every(function(currentValue, index, arr), thisValue)
JavaScript every() Method Example:
In below example, first we initialize an array named heights with some numeric elements. Now use every() method to find if any array element is greater than 25. The function checkHeight() with exit with array element with value 33.
1 2 3 4 5 6 7 8 9 10 | <script type="text/javascript"> var heights = [15, 24, 33, 12]; function checkHeight(a) { return a >= 25; } var result = heights.every(checkHeight); console.log(result); </script> |
1 Comment
The every() method returns true or false based on whether or not every item in the array passes the condition you give in a callback function.