The Array.every method in JavaScript is utilized to determine if every element within the array meets a specified condition, which is defined by a function provided as an argument. This method executes the supplied function on each element of the array, evaluating whether all elements satisfy the given criteria.
Syntax:
array.every(function( Value, Index, arr), thisValue)
Parameters:
Value (Required) : The value of current element.
Index (Optional): Refers to the position within the array of the current elements.
Arr(optional): The array object to which the current element is associated.
this value (optional): A parameter that can be sent to the function.
NOTE: If this parameter is empty, the value "undefined" will be passed as its "this" value.
Return value:
This function yields a Boolean value of true if every element within the array meets the criteria defined by the provided argument function.
If any element within the array fails to meet the specified criterion, the result will be false.
Browser Support:
| Chrome | Yes |
|---|---|
Edge |
Yes |
| Firefox | 1.5 |
| Opera | Yes |
Example 1
<script type="text/javascript">
// JavaScript to illustrate every() method
// Input array
var arr = [200,101,450,789];
function Example(n)
{
return n> 100;
}
document.write(arr.every(checkFunction));
// expected output: true
</script>
Output:
Example 2
<script type="text/javascript">
// JavaScript to illustrate every() method
// Input array
var arr = [200,101,450,789];
function Example(n)
{
return n< 100;
}
document.write(arr.every(checkFunction));
// expected output: false
</script>
Output: