Array.every() – for checking if all items meet a condition

This article is the eighth of the Array Method Series. In this article, I will explain what the every Array method is.

What is the Every Method?

The every method of arrays is a higher-order function that asserts if ALL items in an array mee…


This content originally appeared on DEV Community and was authored by Dillion Megida

This article is the eighth of the Array Method Series. In this article, I will explain what the every Array method is.

What is the Every Method?

The every method of arrays is a higher-order function that asserts if ALL items in an array meet a certain condition. If all items meet it, it returns true, and if at least one item does not meet it, it returns false.

This method does not modify the array. It only loops through and applies a condition on each item until it finds the one that does not match. This means that if it finds an element that does not match, it doesn't continue looping through the remaining items in the array. It immediately returns false. And if it never finds such an item all through the loop, then it returns true

Syntax of the Every Method

array.every(function(item, index, array){
  // condition to test item with
  // return true or false
})

The callbackFunction passed to the every method is applied to each item in the array until it finds the item that does not match the condition in the function.

The arguments passed to the callback function in each loop are the item, the index of the item, and the original array.

Without the Every Method

The every method is an abstracted function that does a quick check and stops at the first item that does not pass a certain criterion. Here's an example imitating the every method:

const array = [1, 2, 3, 4, 5, 6, 7, 8]

let allNumbersLessThan5 = false;

for (let i = 0; i < array.length; i++) {
  const item = array[i]
  console.log(item)
  if (item >= 5) {
    allNumbersLessThan5 = false
    break
  }
}

console.log(allNumbersLessThan5)
// 1
// 2
// 3
// 4
// 5
// false

This loop approach is similar to what the every method does in the background. It loops through each item, and when it finds the item that does not match the specified condition (that all numbers should be less than 5), it stops the loop and returns false.

From the logs, you would see that the loop stopped at 5, and since 5 did not pass the test, then the method already knows that NOT EVERY item meets the condition.

If it doesn't find a reason to stop the loop, then it means that all the numbers are less than 5.

With the Every Method

Here's how you achieve the previous result with every:

const array = [1, 2, 3, 4, 5, 6, 7, 8]

const allNumbersLessThan5 = array.every(item => {
  console.log(item)
  return item < 5
})

console.log(allNumbersLessThan5)
// 1
// 2
// 3
// 4
// 5
// false

Easier to read with fewer lines of code. Let's see an example where everything passes:

const array = [1, 2, 3, 4, 5, 6, 7, 8]

const allNumbersLessThan5 = array.every(item => {
  return typeof item === "number"
})

console.log(allNumbersLessThan5)
// true

Every item passes the condition, so indeed, EVERY item is a number.

The every method is useful when you have different values in an array and you want to assert that all of them meet a condition.


This content originally appeared on DEV Community and was authored by Dillion Megida


Print Share Comment Cite Upload Translate Updates
APA

Dillion Megida | Sciencx (2022-04-26T06:44:39+00:00) Array.every() – for checking if all items meet a condition. Retrieved from https://www.scien.cx/2022/04/26/array-every-for-checking-if-all-items-meet-a-condition/

MLA
" » Array.every() – for checking if all items meet a condition." Dillion Megida | Sciencx - Tuesday April 26, 2022, https://www.scien.cx/2022/04/26/array-every-for-checking-if-all-items-meet-a-condition/
HARVARD
Dillion Megida | Sciencx Tuesday April 26, 2022 » Array.every() – for checking if all items meet a condition., viewed ,<https://www.scien.cx/2022/04/26/array-every-for-checking-if-all-items-meet-a-condition/>
VANCOUVER
Dillion Megida | Sciencx - » Array.every() – for checking if all items meet a condition. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/04/26/array-every-for-checking-if-all-items-meet-a-condition/
CHICAGO
" » Array.every() – for checking if all items meet a condition." Dillion Megida | Sciencx - Accessed . https://www.scien.cx/2022/04/26/array-every-for-checking-if-all-items-meet-a-condition/
IEEE
" » Array.every() – for checking if all items meet a condition." Dillion Megida | Sciencx [Online]. Available: https://www.scien.cx/2022/04/26/array-every-for-checking-if-all-items-meet-a-condition/. [Accessed: ]
rf:citation
» Array.every() – for checking if all items meet a condition | Dillion Megida | Sciencx | https://www.scien.cx/2022/04/26/array-every-for-checking-if-all-items-meet-a-condition/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.