4 minutes read

While working on large projects, you may reach a point where your array contains too many items to remember. You must check whether the array meets certain conditions so the website works properly! This is when array-checking methods come in handy! In this topic, you will learn about using three of these methods: every(), some(), and filter().

Array-checking methods in JavaScript

There are a lot of different methods in JavaScript that work with items in an array. In this topic, we will focus on the methods that check certain conditions for the items in the array. These methods include:

  • every() method that checks whether all the items in an array pass the function test;

  • some() method that checks whether at least one item in an array passes the function test;

  • filter() method that creates a new array filled with elements that pass a test provided by the function.

To showcase how each method works, we will use the same array consisting of different numbers. Let's get started!

Every()

The every() method checks whether all the items in an array pass the function test. In other words, this method executes a function for each item in an array and returns the boolean value. If an item passes the test, it will return true. The method doesn't execute the function for empty elements.

Let's say you have an array with different numbers. You want a number that is greater than 10. To do that, you need a function that checks the age. In this example, the function is called checkNumber which works with the age parameter. After that, you will use the array called numbers in console.log with the every() method that works with checkNumber.

Here is the input:

const numbers = [2, 5, 11, 40];

console.log(numbers.every(checkNumber))

function checkNumber(number) {
  return number > 10;
}

Here is the output:

false

Some()

The some() method checks whether at least one item in an array passes the function test. The same rule applies to the some() method — it returns the boolean value. It returns true if the method finds an item in an array that meets the condition of a function and false, otherwise. The some() method doesn't execute the function for empty array elements.

With this method, we will check if only one number in the array is bigger than 10. We will use the same array and function with every task.

Here is the code input:

console.log(numbers.some(checkNumber))

And the code output:

true

Excellent! Try to play around with the items in the numbers array. Try to switch numbers that are less than 10 and see what output you will get.

Filter()

In this part, you will learn about the filter()method. The filter() method creates a new array filled with elements that pass a test provided by a function. Unlike the other two methods you've studied in this topic, this method will output what you want in the new array! Keep in mind, just like with every() and some() method, this method does not execute the function for empty elements.

So let's say you want all numbers from the numbers array that are bigger than 10.

Here is the input:

console.log(numbers.filter(checkNumber))

Here is what we'll get:

[ 11, 40 ]

Conclusion

In this topic, you learned about different methods for checking the items in an array.

  • every() method checks whether all the items in an array pass the function test,

  • some() method checks whether at least one item in an array passes the function test,

  • filter() method creates a new array filled with elements that pass a test provided by a function.

Now, you can check various items in an array and quickly find what you need. Let's put it to the test!

74 learners liked this piece of theory. 0 didn't like it. What about you?
Report a typo