JavaScript

Array Every() Method in JavaScript

JavaScript every() method is a built-in array method that returns a Boolean result indicating if every element in an array meets a set of conditions. In other words, every() method checks whether your array passes a certain test or not and returns boolean true or false depending on the result of the test.

Suppose you have just one element in your whole array that doesn’t meet the specified condition then every method will short circuit the loop and will break out of every method returning false. If it gets the false value in the middle then every method won’t check for other elements and break out of the loop, hence giving us a performance boost.

It should be noted that every() method does not run for an empty array and it doesn’t change the original array. Also, as mentioned earlier, every method is an array method hence every method should be invoked only through an array object.

Syntax

myArr.every(callback(currentValue,index,arr),thisArg)

every() method takes some parameters which include a call-back function. The callback is a method or function that is supplied as a parameter to another method and begins execution only when the other function is completely executed. Callback functions ensure that the function will not execute unless some task or function has not finished executing. This call-back function runs for every element in the array. The callback function in this case also takes three parameters which are mentioned below:

  • The currentValue is a necessary parameter and it indicates the value of the current element.
  • The index is an optional parameter and indicates the index of the current element in the array
  • The arr is also optional and refers to the array object to which the current element belongs.

The every() method also takes another parameter which is the thisArg and is optional. The thisArg refers to the value which is passed so that it can be used as this value when the callback function is executed.

Example

var numbers=[1,2,3,4,5,6];

function check(currentValue)  {  
 return currentValue5
}  


alert(numbers.every(check));

In the above example, first, we initiated an array with numbers ranging from 1 to 6. Then a function check is initiated which checks a value and returns true or false on the basis that the value should be smaller than 5. In the end, we initiated every() method on the numbers array and passed the callback function check. Now even if one element doesn’t meet the required check that its element value should be less than 5, it will return false. Hence, the first four elements are fine and are less than 5 but when it reaches the value 5, it will short circuit and will stop looking further. We will see false in the output:

Example 2:

var numbers=[1,2,3,4,5,6];

function check(element, index, array)  {  
 return element>0; //returns true as all elements are >0
}

alert(numbers.every(check));

We changed the example 1 code a little bit and modified it by providing the condition that the currentValue or every() element of the array should be a positive number, that is it should be greater than 0. We will see true now in the output:

Browser Compatibility

The every() method is supported by almost every major browser and some of them are listed below:

Browser Google Chrome Microsoft Edge Safari Mozilla Firefox Opera
Supports yes >=9.0 yes >=1.5 yes

Conclusion

The every() method is a JavaScript array method that is used to check for a certain condition on the array elements. It returns a boolean value, that is, if it passes the test for every array element then true is returned, otherwise false is returned. Once it gets an element that doesn’t pass the specified test, then it breaks every() method and returns false without checking the remaining elements.

every() method is better than the for loop as every() method gives a cleaner code and hence enhances the readability and debugging of the code. In this post, we studied the array every() method in JavaScript.

About the author

Shehroz Azam

A Javascript Developer & Linux enthusiast with 4 years of industrial experience and proven know-how to combine creative and usability viewpoints resulting in world-class web applications. I have experience working with Vue, React & Node.js & currently working on article writing and video creation.