JavaScript array methods are one of the most helpful features of this language. In today's article, we'll talk about two specific methods - some() and every() to explore their functionalities and determine their differences.

So, let's dive right in!

How Does the some() Method Work in JavaScript?

The some() method tests whether at least one element in the array satisfies the condition set by the given function. The method returns a Boolean value - it's either true or false.

Consider the following example: We have an array of books and are interested in identifying whether it contains any fantasy genre titles. This is a perfect use case for the some() method.

const books = [
  { title: 'A Song of Ice and Fire', genre: 'Fantasy' },
  { title: 'Forrest Gump', genre: 'Drama' },
  { title: 'The Witcher', genre: 'Fantasy' },
];

console.log(books.some(book => book.genre === 'Fantasy'));  // Output: true

The output is true because our array contains at least one book in the fantasy genre (actually two in this example, but since some() checks for 'at least one,' the exact number doesn't matter).

How Does the every() Method Work in JavaScript?

Like some(), the every() method returns a Boolean value based on a given test function. However, every() checks whether all elements in the array satisfy the specified criteria.

Let's stick with our array of books, but this time, check if all the books belong to the fantasy genre.

const books = [
  { title: 'A Song of Ice and Fire', genre: 'Fantasy' },
  { title: 'Forrest Gump', genre: 'Drama' },
  { title: 'The Witcher', genre: 'Fantasy' },
];

console.log(books.every(book => book.genre === 'Fantasy'));  // Output: false

The output is false here because not all the books in the array fall under the fantasy genre.

A Few Interesting Facts

Short-Circuiting

Both some() and every() will short-circuit, meaning they will stop iterating over the array once they have found a final answer.

const books = [
  { title: 'A Song of Ice and Fire', genre: 'Fantasy' }, // For some() iteration stops here 🛑
  { title: 'Forrest Gump', genre: 'Drama' }, // For every() iteration stops here 🛑
  { title: 'The Witcher', genre: 'Fantasy' },
];

console.log(books.some(book => book.genre === 'Fantasy'));  // Output: true
console.log(books.every(book => book.genre === 'Fantasy'));  // Output: false

In the given example, the some() method stops its iteration as soon as it finds the first element in the array that belongs to the fantasy genre. The every() method stops its iteration as soon as it finds the first element in the array that DOESN'T BELONG to the fantasy genre.

Handling Empty Elements

Both methods can be run on arrays that contain empty elements. These empty slots will be skipped and won't affect the overall result.

// Example 1

const books = [
  { title: 'A Song of Ice and Fire', genre: 'Fantasy' },
  {},
  { title: 'The Witcher', genre: 'Fantasy' },
];

console.log(books.every(book => book.genre === 'Fantasy'));  
// The output will be false because an empty object is still counted as an array
// element, and its missing 'genre' property doesn't match 'Fantasy.'

// Example 2

const books = [
  { title: 'A Song of Ice and Fire', genre: 'Fantasy' },
  ,
  { title: 'The Witcher', genre: 'Fantasy' },
];

console.log(books.every(book => book.genre === 'Fantasy'));  
// The output will be true because empty slots in an array are skipped by the
// 'every()' method, so they don't affect the overall result.

Immutable Methods

It's also worth mentioning that both some() and every() don't alter the original array but instead return a new value.

💡
Also: Take a look at my article on the differences between npm and npx.

Final Words

The differences between some() and every() may seem minor, but understanding them can be quite beneficial. The some() method checks if at least one item in an array meets a given condition, while every() ensures that all items satisfy that condition.

What they have in common is their practicality; both are extremely useful tools that could help in your future projects.

Thanks for taking the time to read this.

Good luck 🖐


Resources 🔗:

💡
Also: Take a look at my article on resetting the local git branch to remote.
Table of Contents
Great! Next, complete checkout for full access to Kajetan Domagała.
Welcome back! You've successfully signed in.
You've successfully subscribed to Kajetan Domagała.
Success! Your account is fully activated, you now have access to all content.
Success! Your billing info has been updated.
Your billing was not updated.