When and Why to use the .every() Array Method in Javascript

If you are not familiar with the .forEach() or .reduce() array methods I suggest you check out Why and when to use forEach, map, filter, reduce, and find in JavaScript.

When to use .every()?

.every() when you want derive a single boolean value from multiple elements in an array.

Conditions:

  • You have an array.
  • You want to test each element in the array.
  • You want to know whether all of the elements pass that test. true / false .

How to use .every()?

Using .every() s similar to .reduce() with the exception that there is an implicit accumulator. i.e. You give .every() a test to run on every element in an array, and it keeps track of whether or not the all return true or false for you.

Array.every(testFunction);

Example:

var sampleArray = [ 1, 2, 3, 4, 5 ];var sampleArray2 = [ 0, -1, -30, 5];function tester(number){ return number > 0;}sampleArray.every(tester); // returns truesampleArray2.every(tester); // returns false

Essentially what this is doing is going through each element of the array and checking to see if they?re all true.

ES6 Example:

A nice benefit with es6 syntax is that you can easily pass an anonymous tester function in a concise manner.

const sampleArray = [ 1, 2, 3, 4, 5 ];const sampleArray2 = [ 0, -1, -30, 5];sampleArray.every( number => number > 0 ); // returns truesampleArray2.every( number => number > 0 ); // returns false

Why use .every()?

Alright so why use .every() ? You could achieve the same thing using .reduce() or .forEach() or a simple for loop. Let?s take compare other implementations.

For Loop Example

let sampleArray = [ 1, 2, 3 ];let i;let isGreaterThanZero = number => number > 0;let allPositive = true;for (i=0; i < sampleArray.length; i++) { allPositive = allPositive && isGreaterThanZero(sampleArray[i]);}

For Of Loop Example

let sampleArray = [ 1, 2, 3 ];let isGreaterThanZero = number => number > 0;let allPositive = true;for (let i of sampleArray) { allPositive = allPositive && isGreaterThanZero(sampleArray[i – 1]);}

.forEach() Example

let sampleArray = [ 1, 2, 3 ];let isGreaterThanZero = number => number > 0;let allPositive = true;sampleArray.forEach( num => { allPositive = allPositive && isGreaterThanZero(num);});

.reduce() Example

let sampleArray = [ 1, 2, 3 ];let isGreaterThanZero = number => number > 0;let allPositive = sampleArray.reduce( (allPositive, num ) => { return allPositive && isGreaterThanZero(num); });

.every() Example

let sampleArray = [ 1, 2, 3 ];let isGreaterThanZero = number => number > 0;let allPositive = sampleArray.every( num => isGreaterThanZero(num));

Though it has a narrower range of uses and maybe not as well known as other array methods, .every() is still a standard array method in Javascript.

I hope that the answer to ?Why use .every()” is clear. Based on the above examples? it?s much easier to read and quickly figure out what is going on when using standard methods. If you don?t use standard methods, the next developer to look at this code will have to spend additional time to figure out what is going on.

Further Reading

Array.prototype.every()

The every() method tests whether all elements in the array pass the test implemented by the provided function.

developer.mozilla.org

24