Understanding Javascript Array Series X - Array Loops & Iteration Part VII
Tags: javascript, webdev, beginners
Published: 2019-10-10 16:27:08

In the previous article, I talked about iterating over arrays using the  `Array.some()` array method. You can check it out below:

{% link https://dev.to/nedyudombat/understanding-javascript-array-series-ix-array-loops-iteration-part-vi-126 %}

Today, I will talk about using **array.every()** to iterate over arrays.

## Every()
  This method checks that every element in an array passes the specified test and returns true to that effect. This is basically like the opposite of `Array.some()`, where it checks that every element passes a test as opposed to `Array.some()` that checks that at least one element passes the test.

In `Array.some()`, while the loop is running, if it returns true then the loop ends and the remaining elements are not processed. Similarly for `Array.every()`, if the method returns false at some point in the iteration, the loop ends and the remaining methods are not processed.

Here we have an array of `[2, 4, 5, 6, 8]` and we want to check if all the elements in the array are even numbers. `Array.every()` would return false because 5 is not an even number. Here is a code sample below:
```javascript
const arr = [2, 4, 5, 6, 8];
console.log(arr.every(num => num % 2 === 0)); // false
```
This loop runs until it gets to 5 where it returns false and the loop ends. 
Let's take a look at the syntax
```javascript
   // syntax
   arr.every(([currentValue], [arrayIndex], [arr]) => {
     // [specified condition]
   });

   or

   arr.every(callback([currentValue], [arrayIndex], [arr]));
```
**[currentValue]:** This is the current item in the array that is being processed. After the procession, the current value becomes the value of the next element in the array.

**[arrayIndex]:** This is the index of the current value in the array. This also changes after the current value has been processed.

**[arr]**: This is the array being iterated over.

**[callback]**: This is basically a function to be performed on each element of the array. It accepts the first three items (currentValue, index, and array) as arguments.

Here are similar examples from yesterday's article:

* We have an array of ages of members of a family going into a theatre to watch a movie rated PG 13+. We want to perform a check to ensure that everyone is over 13 years:
```javascript
const array = [12, 14, 29, 23, 25, 18];
console.log(array.every(num => num > 13)); // false
```
Unfortunately, there is a twelve-year-old kid, so we cannot allow the family into the theatre :grin:

* Alternatively we can also check if an array of kids going to play are of the correct age limit.
```javascript
const array = [8, 10, 9, 8, 12, 11];
console.log(array.every(num => num < 18)); // true
```
This returns true because all the kids are under the age of 18.



## **Conclusion**
  `Array.every()` is great when you want to check that every item in an array meets a particular criterion. Alternative if you want to check that at least one item in that array meets that criterion you should use [`array.some()`](https://dev.to/nedyudombat/understanding-javascript-array-series-ix-array-loops-iteration-part-vi-126). 

Got any other instances for the use of the `Array.every()` function? Please do well to share it in the comment section.

That's all for today, tomorrow we will talk about another set of functions used in array Iteration.

Here is the link to the other articles on this Array series written by me:
* [What is an Array?](https://dev.to/nedyudombat/understanding-javascript-array-series-i-what-is-an-array-l1i)
* [Alternate ways of Creating an Array.](https://dev.to/nedyudombat/understanding-javascript-array-series-ii-alternate-ways-of-creating-an-array-26c8)
* [Array Properties](https://dev.to/nedyudombat/understanding-javascript-array-series-iii-array-properties-43co)
* [Array Loops & Iteration Part I] (https://dev.to/nedyudombat/understanding-javascript-array-series-iv-array-loops-iteration-part-i-334j)
* [Array Loops & Iteration Part II] (https://dev.to/nedyudombat/understanding-javascript-array-series-iv-array-loops-iteration-part-ii-115o)
* [Array Loops & Iteration Part III] (https://dev.to/nedyudombat/understanding-javascript-array-series-iv-array-loops-iteration-part-iii-4f38)
* [Array Loops & Iteration Part IV] (https://dev.to/nedyudombat/understanding-javascript-array-series-vii-array-loops-iteration-part-iv-3nb8)
* [Array Loops & Iteration Part V] (https://dev.to/nedyudombat/understanding-javascript-array-series-viii-array-loops-iteration-part-v-5had)
* [Array Loops & Iteration Part VI] (https://dev.to/nedyudombat/understanding-javascript-array-series-ix-array-loops-iteration-part-vi-126)

Got any question, addition or correction? Please leave a comment.

Thank you for reading. :thumbsup: