JavaScript Array every() Method
Example
Check the array ages whether all of its elements are greater than or equal to 18:
var
ages = [
32
,
33
,
16
,
40
];
function
checkAdult(age) {
return
age >=
18
;
}
function
myFunction() {
document.getElementById(
"demo"
).innerHTML = ages.every(checkAdult);
}
The output result is:
false
Try it »
Definition and Usage
The every() method checks whether all elements in an array pass a specified condition (provided by a function).
The every() method uses the specified function to test all elements in the array:
- If one element in the array fails the test, the whole expression returns false , and the remaining elements will not be tested.
- If all elements satisfy the condition, it returns true.
Note: every() does not check empty arrays.
Note: every() does not change the original array.
Browser support
The numbers in the table indicate the first browser version that supports this method.
| Method | |||||
|---|---|---|---|---|---|
| every() | Yes | 9 | 1.5 | Yes | Yes |
Syntax
array.every(function(currentValue,index,arr), thisValue)
Parameter Description
| Parameters | Description |
|---|---|
| function(currentValue, index,arr) | Required. A function that is executed for each element in the array |
函数参数:
Parameter Description currentValue Required. The value of the current element index Optional. The index of the current element arr Required. The array object to which the current element belongs | Parameter | Description | *currentValue* | Required. The value of the current element | *index* | Optional. The index of the current element | *arr* | Optional. The array object to which the current element belongs | | Parameter | Description | | *currentValue* | Required. The value of the current element | | *index* | Optional. The index of the current element | | *arr* | Optional. The array object to which the current element belongs | | *thisValue* | Optional. An object passed to the function as the value of "this" when the callback is executed. If thisValue is omitted, the value of "this" is "undefined" |
Technical Details
Return Value:
Boolean. Returns true if all elements pass the test, otherwise returns false.
JavaScript version:
1.6
More examples
Example
Check whether all elements of the array ages are greater than or equal to the number specified in the input box:
Minimum age: "number"
id=
"ageToCheck"
value=
"18"
JavaScript Array object