The JavaScript every Method

May 19, 2015
JavaScript

For those of you not familiar, Array.prototype.every() is a method that tests every element in an array against whatever callback function you pass in.

It’s perhaps most similar to Array.prototype.filter() but there are two key differences:

  1. It returns a boolean whereas filter returns a new Array.
  2. The boolean is determined by whether or not all items in the array pass the test function.

A Quick Example

function isEven(number, index, array) {
  return number % 2 === 0;
}
console.log([10, 11, 12, 13, 14].every(isEven)); // false
console.log([10, 12, 20, 22, 30].every(isEven)); // true

As you can see, every returns false if any item in the array is odd.

Comparing Map and Filter

Where Map and Filter return new arrays, every just returns a boolean. This means that every won’t work for method chaining and can only be used to make sure every item in your array passes a certain test.