JavaScript Array every() Method

The every() method tests whether all elements in the array pass the test implemented by the provided function. It returns true if the function returns true for every array element. If any element fails the test, it returns false.

Syntax

javascript
array.every(callback(element[, index[, array]])[, thisArg])

Parameters

  • callback: A function that tests each element in the array. It is invoked with three arguments:
    • element (Required): 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): An object to use as this when executing the callback function.

Return Value

The every() method returns:

  • true if the callback function returns true for every element in the array.
  • false if the callback function returns false for at least one element in the array.

Example 1. Basic Usage of Array every() Method

javascript
const numbers = [2, 4, 6, 8, 10];
const allEven = numbers.every(num => num % 2 === 0);

console.log(allEven); // Output: true

Explanation:

  • The every() method tests if all numbers in the array are even.
  • The callback function num => num % 2 === 0 checks if each number is divisible by 2.
  • Since all elements in the array are even, the output is true.

Example 2: Checking for All Positive Numbers

javascript
const numbers = [1, 2, 3, 4, 5];
const allPositive = numbers.every(num => num > 0);

console.log(allPositive); // Output: true

Explanation:

  • The callback function num => num > 0 checks if each number is positive.
  • Since all numbers are positive, the output is true.

Example 3: With Empty Arrays

javascript
const emptyArray = [];
const result = emptyArray.every(num => num > 0);

console.log(result); // Output: true

Explanation:

  • For an empty array, every() returns true by definition, as there are no elements that fail the test.

Example 4: Using thisArg

javascript
const person = {
  ageLimit: 18,
  ages: [16, 21, 18],
  checkAge: function() {
    return this.ages.every(age => age >= this.ageLimit);
  }
};

console.log(person.checkAge()); // Output: false

Explanation:

  • thisArg is used to refer to the person object within the checkAge method.
  • The every() method tests if all ages are greater than or equal to the age limit.
  • Since 16 is less than 18, the output is false.

Example 5: Testing Non-Primitive Values

javascript
const objects = [{ value: 1 }, { value: 2 }, { value: 3 }];
const allHaveValues = objects.every(obj => obj.value > 0);

console.log(allHaveValues); // Output: true

Explanation:

  • The every() method tests if the value property of all objects is greater than 0.
  • All objects meet the condition, so the output is true.

Example 6: Checking for Array Elements

javascript
const strings = ['apple', 'banana', 'cherry'];
const allStrings = strings.every(str => typeof str === 'string');

console.log(allStrings); // Output: true

Explanation:

  • The callback function str => typeof str === 'string' checks if every element is a string.
  • Since all elements are strings, the output is true.

Example 7: Handling Mixed Data Types

javascript
const mixedArray = [1, 'two', 3, 'four'];
const allNumbers = mixedArray.every(
	item => typeof item === 'number');

console.log(allNumbers); // Output: false

Explanation:

  • The callback function item => typeof item === 'number' checks if every item is a number.
  • Since the array contains strings, the output is false.

JavaScript

5179

726

Related Articles