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
This time, let's cover the
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 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
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 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
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.
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!
- Filtering Arrays with Fat Arrows
- Discoverability, Semantic Markup & Accessibility
- Bias in Artificial Intelligence Confirmed
I am human, a father, and a problem solver: an inclusive tech and people leader with a passion and proven track-record in recruiting and leading compassionate, productive teams—remote and on-site—within a continuous learning culture. My teams and I champion usable, accessible digital products and online experiences. My work, passion and values also intersect with advising small businesses and political campaigns, outdoor and education advocacy, community building, and amplifying voices from historically marginalized communities. Learn more about Manny
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.