JavaScript Array every() method

The JavaScript array every() method checks whether all the given elements in an array are satisfying the provided condition. It returns a Boolean value.

The every() method executes a function for each array element.

The every() method does not change the original array.

The every() method does not execute the function for empty elements.

The every() method returns true if the function returns true for all elements.

The every() method returns false if the function returns false for one element.

Syntax

The syntax of the every() method is:

array.every(callback(currentValue,index,arr),thisArg)  

Parameters: This method accepts five parameters as mentioned above and described below:

callback – Required. It represents the function that test the condition.

currentValue – Required. The current element of array.

index – It is optional. The index of current element.

arr – It is optional. The array on which every() operated.

thisArg – It is optional. The value to use as this while executing callback. By default, it is undefined.

Return Value

A Boolean value. true if all elements pass the test, otherwise false.

Examples

function isBigEnough(element, index, array) {
  return element >= 10;
}
[12, 5, 8, 130, 44].every(isBigEnough);   // false
[12, 54, 18, 130, 44].every(isBigEnough); // true
function checkAdult(age) {
    return age >= 18;
}

const ageArray = [34, 23, 20, 26, 12];
let check = ageArray.every(checkAdult); // false

if (!check) {
    console.log("All members must be at least 18 years of age.")
}

// using arrow function
let check1 = ageArray.every(age => age >= 18); // false
console.log(check1);