JavaScript Array every() method


The every() method in JavaScript is used to test whether all elements in an array pass the test implemented by the provided callback function. It returns a boolean value: true if all elements satisfy the condition, and false otherwise.

Syntax:

let result = array.every(callback(currentValue, index, array), thisArg)
  • callback: A function that is called for each element in the array. It can accept up to three parameters:

    • currentValue: The current element being processed in the array.
    • index (optional): The index of the current element being processed.
    • array (optional): The array every() was called upon.
  • thisArg (optional): A value to use as this when executing the callback function.

Return Value:

  • A boolean value (true or false):
    • true: If the callback function returns a truthy value for all elements in the array.
    • false: If the callback function returns a falsy value for at least one element in the array.

Key Points:

  • Stops processing on the first falsy return: Once a falsy value is found, every() stops checking the remaining elements and returns false.
  • Does not modify the original array: The every() method does not alter the original array; it only checks the values based on the callback function.
  • Commonly used for validation: It is often used to ensure that all elements in an array meet certain criteria.

Example 1: Basic usage (Checking for positive numbers)

let numbers = [1, 2, 3, 4, 5]; let allPositive = numbers.every(function(number) { return number > 0; // Check if all numbers are greater than 0 }); console.log(allPositive); // true

Example 2: Using an arrow function (Checking for an object property)

let users = [ { id: 1, name: 'Alice', active: true }, { id: 2, name: 'Bob', active: true }, { id: 3, name: 'Charlie', active: true } ]; let allActive = users.every(user => user.active); // Check if all users are active console.log(allActive); // true

Example 3: Returning false when any match fails

let numbers = [1, 2, 3, -4, 5]; let allPositive = numbers.every(function(number) { return number > 0; // Check if all numbers are greater than 0 }); console.log(allPositive); // false

Example 4: Using index and array parameters

let numbers = [2, 4, 6, 8]; let allEven = numbers.every(function(element, index) { return element % 2 === 0; // Check if all elements are even }); console.log(allEven); // true

Example 5: Using thisArg

let threshold = { max: 10 }; let numbers = [3, 5, 7, 9]; let allUnderThreshold = numbers.every(function(num) { return num < this.max; // Check if all numbers are less than threshold.max }, threshold); console.log(allUnderThreshold); // true

Summary:

  • The every() method is a useful way to validate that all elements in an array meet specific criteria defined in a callback function.
  • It returns a boolean indicating whether all elements satisfy the condition, making it useful for checks and validations within collections of data.
  • The method is particularly effective when working with arrays of objects, allowing for straightforward queries based on object properties.