JavaScript: Array every() method

JavaScript Array every method;In this tutorial, you will learn how to check whether all the given elements in an array are fulfilled the given test condition.

When you work with javascript arrays, so many times, you need to test whether all elements of an array and meet a specific test condition.

So in that case, you iterate all elements of an array using for loop and check the specific condition on each element of an array.

For example, you have numeric array, see the following:

let numbers = [1, 3, 5, 7, 9];

The following javascript code satisfies specific test condition on every element of a numeric array:

let numbers = [1, 3, 5, 7, 9];
let output = true;
for (let i = 0; i < numbers.length; i++) {
    if (numbers[i] <= 0) {
        output = false;
        break;
    }
}
console.log(output);

Output:

true

How it works:

  • First of all, declare a numeric array with elements.
  • Next, define the  output variable and assign value to true.
  • After that, iterate for loop over all elements and check specific test condition on each element of an array using the if statement.
    • If the specified test condition met true, the output variable assing to false and terminate the for loop quickly using the break statement.
    • Otherwise, the value of the output variable will be true.
  • Console.log() the output variable.

JavaScript every() method that allows you to check each element of an array with specified a test condition in a more concise and more readable way.

JavaScript Array every() method

Javascript Array every() method, check whether all the array elements are fulfilled the given test condition. It returns true when each given array element fulfills the test condition, otherwise return false.

In ES5, JavaScript Array every() method, tests each element of array with specific test condition with function.

The following example of javascript every() method:

let numbers = [1, 3, 5, 7, 9];
let output = numbers.every(function (e) {
    return e > 0;
});

console.log(output);

Output:

true

By using the JS ES6 arrow functions, the javascript code can be more concise and more readable:

let numbers = [3, 5, 9, 11, 15];
let result = numbers.every(e => e > 0);

console.log(result); // true

Syntax of javascript every() method

The following represents the syntax of the every() method.

arrayObject.every(callback[, thisArg])

The every() method accepts two named parameters: callback and thisArg.

1) The callback parameter

The callback is a function that tests each element of the array.

See the following:

function callback(currentElement, index, array){
   //...
}

The callback() function accepts 3 parameters:

  • First, the currentElement is the current element of a given array.
  • Second, the index is the index  of the current element of a given array
  • Third, the array is the given array.

2) The thisArg parameter

The thisArg parameter of the every() method is optional. If you pass the thisArg parameter into the method. So inside this callback function will get the reference this .

The every() method returns true if the callback function returns a truthy value for each array element; otherwise, it returns false.

Note that the every() method executes the callback() function on every element in the array until it finds the one that causes the callback() return a falsy value.

Examples: JavaScript Array every() method

The following example checks whether all the array elements are even numbers in it:

let numbers = [3, 5, 9, 11, 15];
let isEven = numbers.every(function (e) {
    return e % 2 == 0;
});

console.log(isEven);

Output:

false

The following example checks whether all the array elements are odd numbers in it:

let numbers = [3, 5, 9, 11, 15];
let isOdd = numbers.every(function (e) {
    return Math.abs(e % 2) == 1;
});

console.log(isOdd);

Output:

true

If you use the every() method on an empty array, the method will always return true for any condition.

See the following example:

let num = [].every(e => e > 0); // any condition

console.log('num:', num);

Output:

num: true

Conclusion

In this tutorial, you have learned how to use the JavaScript Array every() method to checks whether each element of an array and test defined condition.

Recommended JavaScript Tutorials

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *