Easy Tutorial
❮ Prop Html Classname Prop Nav Product ❯

JavaScript Array every() Method

JavaScript Array Object

Example

Check if all elements in the ages array are 18 or older:

var ages = [32, 33, 16, 40];
function checkAdult(age) {
    return age >= 18;
}
function myFunction() {
    document.getElementById("demo").innerHTML = ages.every(checkAdult);
}

Output result:

false

Definition and Usage

The every() method is used to check if all elements in the array pass the test implemented by the provided function.

The every() method tests all elements in the array with the specified function:

Note: every() does not execute the function for array elements without values.

Note: every() does not change the original array.


Browser Support

The numbers in the table specify the first browser version that fully supports the method.

Method Chrome IE Firefox Safari Opera
every() Yes 9 1.5 Yes Yes

Syntax

array.every(function(currentValue, index, arr), thisValue)

Parameter Details

Parameter Description
function(currentValue, index, arr) Required. A function to be run for each element in the array. <br>Function parameters: <br> - currentValue: Required. The value of the current element. <br> - index: Optional. The index of the current element. <br> - arr: Optional. The array object the current element belongs to.
thisValue Optional. A value to be passed to the function to be used as its "this" value. <br>If this parameter is empty, the value "undefined" will be passed as its "this" value.

Technical Details

Return Value: Boolean. Returns true if all elements pass the test, otherwise false.
JavaScript Version: 1.6

More Examples

Example

Check if all elements in the ages array are greater than or equal to the number specified in the input field:

<p>Minimum age: <input type="number" id="ageToCheck" value="18"></p>
<button onclick="myFunction()">Click me</button>
<p>Are all ages meeting the condition? <span id="demo"></span></p>
<script>
var ages = [32, 33, 12, 40];
function checkAdult(age) {
    return age >= document.getElementById("ageToCheck").value;
}
function myFunction() {
    document.getElementById("demo").innerHTML = ages.every(checkAdult);
}
</script>

JavaScript Array Object

❮ Prop Html Classname Prop Nav Product ❯