.every() Method
Use the .every()
method if you want to check if every element in the array passes a condition. In this example, we'll check to make sure that everyone that has registered is above the age of 18.
var ages = [ 34, 27, 43, 18, 59, 22, 40, 25 ]
var over18 = ages.every(function(element) {
return element > 18;
});
console.log(over18);
Our output was false
. The fourth element in our array is 16, which is obviously not greater than 18. As a result the entire array is false. If you got rid of 16, the array would return true. Because our function is element > 18
an element with a value of exactly 18 would not work either.
Last updated