The some() and every() methods in JavaScript

Anastasia Orlova
Dev Genius
Published in
2 min readDec 7, 2020

--

Sometimes, while studying more sophisticated concepts, we tend to forget some basic methods and approaches. so, this week I was revising some of the most commonly used array methods in JavaScript. In this short blog, I’ll show you the difference between some() and every() methods that are often confused between each other.

The MDN documentation says that:

  • The every() method tests whether all elements in the array pass the given condition implemented by the provided function. It returns a Boolean value — true or false.
  • The some() method tests whether at least one element in the array passes the given condition implemented by the provided function. It returns a Boolean value.

To bring these theoretical concepts to life, let’s have a look at this small code snippet.

On lines 1–6 we have an array of people; all of them have a name and a birth year. Our task is to find out:

  1. Is there at least one person 19 years old or older?
  2. Is everyone 19 years or older?

For the first task, we implement some() method (lines 9–13). Using the arrow function from JS ES6 makes our code look neater. By the way, getFullYear() method is really handy! It returns the year of the specified date according to local time.

When we console.log our variable, it returns true — which is right!

For the second task, we use every() method (lines 15–19). The logic is pretty much the same! And you absolutely can use any other JS syntax.

That was my quick review of some() and every(). Hope you enjoyed it!

Source:

  1. MDN web docs: Array.prototype.some()
  2. MDN web docs: Array.prototype.every()
  3. Javascript30

--

--