Beyond the Usual, Passive DEI Efforts in Tech

Blog:JavaScript Array Methods: Every & Some

Loading...
Manny Becerra as a child

July 2018

In an earlier post, I covered the array reduce method to transform array data into a new, single value. For instance, you can begin with one array of numbers that are reduced to a new set of numbers or values based on a product feature requirement.

Both array methods return a boolean value, however, there's a subtle but important distinction between every and some.

This time, let's cover the some and every methods. Both of these methods return a boolean value based on a predicate (or condition). They're pretty handy if you simply need to check an array to determine if a value or condition exists within your array's data. There's a subtle but important distinction between the two methods though, let's take a look.

Some method

The some method only cares if at least one item in an array passes the predicate (or condition). Here's an arbitrary example using the Fibonacci sequence to determine if the list includes the number 4:

const myListOfNumbers = [1, 2, 3, 5, 8, 13, 20, 33, ..., 987, ...] // ... is not a real value, used for brevity
const does4Exist = myListOfNumbers.some(num => num === 4) // Would return false

Since the number 4 is clearly not in our array, our variable does4Exist would be false. You can change the condition some evaluates on each array item to be anything else, for instance, you could check if some of the numbers in the set are even, e.g. myListOfNumbers.some(num => num % 2). In this case, we would get true as a final return value using the some method because there is at least one number in our sample list that is an even number.

Pro tip: for expensive calculations, like checking a large list consisting of the Fibonacci sequence, leverage memoization.

Every method

The every method requires every item in your array to be truthy in regard to your predicate or condition. If at least one item is falsy, every will return false. Using the Fibonacci sequence again, let's take a look at an example where we're wanting to know if every number in our array is even:

const myListOfNumbers = [1, 2, 3, 5, 8, 13, 20, 33, ..., 987, ...] // ... is not a real value, used for brevity
const isEveryNumEven = myListOfNumbers.some(num => num % 2) // Would return false

Our variable, isEveryNumEven, would be false because not every number in the Fibonacci sequence is an even number. Like the some method, your predicate, or condition, can be anything else you want it to be, e.g. you could check if every number is odd, or you could compare one list to another to validate if every value in list A is in list B for instance.



I hope this helps shed some light on these two JavaScript array methods, every and some. Give them a try, if you're not yet too familiar with them. The MDN docs are a great resource for more information on these and other array methods, but don't hesitate to reach out if you have any questions along the way!

Related:


Loading...
Manny Becerra as a child

I operate from a place of compassion, possibility and imagination. My work and efforts share a common goal: create a better, sustainable and equitable world by building inclusive communities, products & experiences.