Javascript: Query an array without looping

In JavaScript there’s some Array methods, like forEach(), indexOf(), map(), reduce()… But today I want to show you (if you don’t know them), three methods that seem to be less frequently used to query an array “without looping”. I put it in quotes because it is not entirely true: you don’t make a loop, but internally the loop is created.

Let’s get down to business: some(), every() and find()

  1. SOME()

This method determines if one or more values are in your array and return false or true, depending on whether the values match. But it is better to show an example:

Imagine that you have some clients and you want to send an email only to those who have pets:

To find if any client has a pet and not use a loop, you would surely use forEach()and your code will be something like this:

But you can have the same result with less lines, using some():

NOTE!

Some() stops iterating over the array as soon as it finds an element that matches the condition. If it finds an element that matches the condition, it automatically returns true without inspecting the remaining elements. In my example above, some() runs 3 times because “Raj Koothrappali” has a pet (true)

And, if every results return false, some() will return false.

2. EVERY()

Too very similar to some() is every(). This function is used to search too if any value matches with the condition. BUT the difference is that every() will return true only if every occurence matches. So we can say that every() search if EVERY element matches the condition.

Following the above example, I’m going to replace some() with every():

You can use it, for instance, if you want to send an email to all clients of legal age, and your code will be something like this:

And like some(), the execution of every() stops as soon as every() finds an array element that doesn’t match the condition and automatically returns false and doesn’t iterate over the remaining elements.

3. FIND()

This method does what it says: find what you are looking for in an array. It returns the first value that meets a certain condition. If there is no value that meets the condition, it returns undefined.

It’s very similar to every() and some(), the difference is that these methods return a boolean, and find() instead of returning a boolean, it will return the whole element in your array.

It’s always better to see an example. Following our clients array, we want to see if there is a person with less than 18 years.

NOTE!

Like I said, it will only return the first match, so if you need all values that match your condition you should use filter() instead of find(). For example, we want to see everybody in our array with legal age:

To sump up, use some(), every() and find() instead of for() or forEach() not only makes your code shorter, it also makes your code clearer, where your intentions are better seen.

Junior frontend developer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store