JavaScript: Array every() method

JavaScript Array every method; Through this tutorial, i am going to show you how to javaScript array.every() for check whether all the given elements in an array are satisfied the given test condition.

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.

Syntax of javascript every() method

The following represents the syntax of the every() method; as shown below:

arrayObject.every(callback[, thisArg])

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

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

Example 1 – JavaScript Determining If All Array Elements Pass a Test

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

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

Example 2 – JavaScript Es6 Array every() method

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

Example 3 – 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.

More JavaScript Tutorials

Recommended:-JavaScript Arrays

Be First to Comment

Leave a Reply

Your email address will not be published.