The every()
method check if all array values pass a test.
This example check if all array values are larger than 18:
Example
const numbers = [45, 4, 9, 16, 25];
let allOver18 = numbers.every(myFunction);
function myFunction(value, index, array) {
return value > 18;
}
let allOver18 = numbers.every(myFunction);
function myFunction(value, index, array) {
return value > 18;
}
Note that the function takes 3 arguments:
- The item value
- The item index
- The array itself
When a callback function uses the first parameter only (value), the other parameters can be omitted:
Example
const numbers = [45, 4, 9, 16, 25];
let allOver18 = numbers.every(myFunction);
function myFunction(value) {
return value > 18;
}
let allOver18 = numbers.every(myFunction);
function myFunction(value) {
return value > 18;
}
Array.every()
is supported in all browsers except Internet Explorer 8 or earlier. Practice Excercise Practice now