JavaScript Array every()
Example
Check if all the values in ages[] are 18 or over:
const ages = [32, 33, 16, 40];
ages.every(checkAge) // Returns false
function checkAge(age) {
return age > 18;
}
Try it Yourself »
More "Try it Yourself" examples below.
Definition and Usage
The every() method returns true if all elements in an array pass a
test (provided as a function).
The method executes the function once for each element present in the array:
- If it finds an array element where the function returns a false value, every() returns false (and does not check the remaining values)
- If no false occur, every() returns true
every() does not execute the function for empty array elements.
every() does not change the original array
Browser Support
every() is fully supported in all modern browsers:
| Chrome | IE | Edge | Firefox | Safari | Opera |
| Yes | 9.0 | Yes | Yes | Yes | Yes |
Syntax
array.every(function(currentValue, index, arr), thisValue)
Parameter Values
| Parameter | Description | ||||||||
|---|---|---|---|---|---|---|---|---|---|
| function(currentValue, index, arr) | Required. A function to be run for each element in the array. Function arguments:
|
||||||||
| thisValue | Optional. A value to be passed to the function to be used as its "this" value. If this parameter is empty, the value "undefined" will be passed as its "this" value |
Technical Details
| Return Value: | A Boolean. Returns true if all the elements in the array pass the test, otherwise it returns false |
|---|---|
| JavaScript Version: | ECMAScript 5 |
More Examples
Example
Check if all the values in an array are a over a specific number:
<p><input type="number" id="ageToCheck" value="18"></p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
const ages = [32, 33, 12, 40];
function checkAge(age) {
return age > document.getElementById("ageToCheck").value;
}
function myFunction() {
document.getElementById("demo").innerHTML = ages.every(checkAge);
}
</script>
Try it Yourself »
Example
Check if all the answers are the same:
const survey = [
{ name: "Steve", answer: "Yes"},
{ name: "Jessica", answer: "Yes"},
{ name: "Peter", answer: "Yes"},
{ name: "Elaine", answer: "No"}
];
survey.every(isSameAnswer) // Returns false
function isSameAnswer(el, index, arr) {
if (index === 0) {
return true;
} else {
return (el.answer === arr[index - 1].answer);
}
}
Try it Yourself »

