DEV Community

Nedy Udombat
Nedy Udombat

Posted on • Updated on

Understanding Javascript Array Series X - Array Loops & Iteration Part VII

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

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:

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

   // 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:
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 😁

  • Alternatively we can also check if an array of kids going to play are of the correct age limit.
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().

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:

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

Thank you for reading. 👍

Top comments (4)

Collapse
 
mercytunde profile image
Tunde Ajagbe

Nice series. These are helpful, not just for beginners but also as a cheatsheet for 'forgeters' 😂

Collapse
 
nedyudombat profile image
Nedy Udombat

Lol... Yes please

Collapse
 
__naaza profile image
Naza

Is this normal JavaScript, can I use this knowledge when learning nodeJs🤔

Collapse
 
nedyudombat profile image
Nedy Udombat

Yes you can.