JavaScript Array every() - Test All Elements

Updated on November 28, 2024
every() header image

Introduction

The every() method in JavaScript is a high-order function particularly useful for validating all elements in an array against a specific condition. Employed mainly for ensuring that every item in an array passes a particular test, this method is essential when you need uniformity across array elements. It returns a Boolean value, making it highly effective for checks that require a straightforward true or false decision.

In this article, you will learn how to utilize the every() method in JavaScript for various practical applications. Explore how this method ensures all elements in an array meet a set criterion, using clear examples that demonstrate its usage in day-to-day coding tasks.

Understanding the every() Method

Basic Usage of every()

  1. Start with defining an array.

  2. Use the every() method and pass a test function to it.

    javascript
    const numbers = [10, 20, 30, 40, 50];
    const isAboveNine = numbers.every(number => number > 9);
    console.log(isAboveNine);
    

    This code evaluates whether each element in the numbers array is greater than 9. Because all elements meet this criterion, every() returns true.

Validating All Elements for a Specific Condition

  1. Prepare an array with object elements.

  2. Invoke the every() method with a condition to check a specific property.

    javascript
    const users = [
        { name: "Alice", age: 25 },
        { name: "Bob", age: 34 },
        { name: "Charlie", age: 47 }
    ];
    const areAllAdults = users.every(user => user.age >= 18);
    console.log(areAllAdults);
    

    Above, every() checks if the age property of each user object in the array satisfies the adulthood condition (being at least 18 years old). In this case, the result is true.

Testing Data Types and Content with every()

Check for Uniform Data Types

  1. Define an array containing different types of data.

  2. Apply the every() method to test if all items are of the same type.

    javascript
    const mixedArray = [1, 'text', true, 20];
    const allNumbers = mixedArray.every(item => typeof item === 'number');
    console.log(allNumbers);
    

    The function checks if every item in mixedArray is a number. As mixedArray includes various types, the function returns false.

Verifying the Presence of Custom Conditions

  1. Create an array to hold a complex condition.

  2. Use the every() method to verify this condition.

    javascript
    const values = [2, 4, 6, 8, 10];
    const allEven = values.every(value => value % 2 === 0);
    console.log(allEven);
    

    This snippet checks if every number in the values array is even. As all elements satisfy this condition, the result is true.

Conclusion

The every() method in JavaScript offers a robust solution for ensuring that every element in an array meets a specific condition. By returning a boolean, it provides a clear, binary answer that is perfect for many tests, from type-checking to more complex conditional evaluations. Implement the every() method in your next project to enforce consistency and correctness in data handling and decision-making processes. Through the examples provided, streamline your coding efforts by using this efficient and effective array method.