DEV Community

Muhammad Rizwan Ashiq
Muhammad Rizwan Ashiq

Posted on

Array.every() Method

What is Every Method?

The every() tests whether all elements in the array pass the test implemented by the provided function. It returns a Boolean value.

Syntax

Here's the syntax for using every():

const result = arr.every((element, index, array) => {
    // Return true if all elements pass the test, false otherwise
});
Enter fullscreen mode Exit fullscreen mode

The callback function is called for each element in the array and takes the following arguments:

  • element: the current element being processed in the array
  • index (optional): the index of the current element
  • array (optional): the array that every() is being applied to

The every() method calls the callback function once for each element in the array, in order, and stops when it finds an element for which the callback returns false. If such an element is found, the method returns false. Otherwise, if the callback returns true for all elements, every() returns true.

Here's an example of how to use every() to check if all the numbers in an array are even:

const numbers = [2, 4, 6, 8, 10];

const result = numbers.every(function (number) {
    if (number % 2 === 0) {
        return true;
    } else {
        return false;
    }
});

console.log(result); // prints true
Enter fullscreen mode Exit fullscreen mode

In the example above, the callback function is an anonymous function that takes a single argument, number, and returns true if number is even, and false otherwise. The every() method calls this function for each element in the numbers array and stops when it finds an odd number. Since all the numbers in the array are even, the every() method returns true.

You can also use an arrow function as the callback function, like this:

let result = numbers.every((number) => number % 2 === 0);

console.log(result); // prints true
Enter fullscreen mode Exit fullscreen mode

The every() method is useful for checking if all the elements in an array pass a certain condition.

Examples

Here are a few more examples of how you can use every():

Checking if all the numbers in an array are greater than 10

const numbers = [12, 14, 16, 18, 20];

const result = numbers.every((number) => number > 10);

console.log(result); // prints true
Enter fullscreen mode Exit fullscreen mode

Checking if all the strings in an array are of a certain length

const strings = ["hello", "world", "this", "is", "javascript"];

const result = strings.every((string) => string.length > 5);

console.log(result); // prints false
Enter fullscreen mode Exit fullscreen mode

Checking if all the elements in an array are of a certain type

const mixedArray = [1, 2, 3, "hello", "world", true, false];

const result = mixedArray.every((element) => typeof element === "number");

console.log(result); // prints false
Enter fullscreen mode Exit fullscreen mode

Checking if student is passed in all subjects

const ross = {
    name: "Ross",
    age: 14,
    class: 8,
    subjects: [
        { name: "Math", score: 90 },
        { name: "Computer", score: 50 },
        { name: "English", score: 80 },
        { name: "Social Science", score: 60 },
        { name: "Science", score: 70 },
    ],
};

const result = ross.subjects.every((subject) => subject.score >= 60);

console.log(result); // prints false
Enter fullscreen mode Exit fullscreen mode

Checking if all the elements in an array are unique

const numbers = [1, 2, 3, 4, 5];

const result = numbers.every((number, index, array) => {
    return array.indexOf(number) === index;
});

console.log(result); // prints true
Enter fullscreen mode Exit fullscreen mode

When to use Every Method?

The every() method is useful for checking if all the elements in an array pass a certain condition. It returns true if all the elements pass the test, and false otherwise. For example:

const numbers = [12, 14, 16, 18, 20];

const result = numbers.every((number) => number > 10);

console.log(result); // prints true
Enter fullscreen mode Exit fullscreen mode

In the example above, the every() method checks if all the numbers in the numbers array are greater than 10. Since all the numbers in the array are greater than 10, the every() method returns true.

Conclusion

In this article, we learned about the every() method. We learned about its syntax, how to use it, and when to use it. We also learned about a few examples of how to use it.

Top comments (0)