Get super confident at JavaScript. Javascript Icon

I promise to change your JavaScript skills!

Exploring Array Some in JavaScript

Follow along with the Exploring JavaScript Array Methods series!

What is Array Some?

Array Some is a method that exists on the Array.prototype that was introduced in ECMAScript 5 (ES5) and is supported in all modern browsers.

Array Some tells you whether any element in your array passes your test. If one element passes then Array Some returns true. Some will return false if no elements pass the test.

As soon as Some finds a true result, it will short-circuit the loop and continue no more - giving us a performance boost.

Think of Array Some as: “I want to check if any value(s) in my array meets my condition - a yes/no answer”

Angular Directives In-Depth eBook Cover

Free eBook

Directives, simple right? Wrong! On the outside they look simple, but even skilled Angular devs haven’t grasped every concept in this eBook.

  • Green Tick Icon Observables and Async Pipe
  • Green Tick Icon Identity Checking and Performance
  • Green Tick Icon Web Components <ng-template> syntax
  • Green Tick Icon <ng-container> and Observable Composition
  • Green Tick Icon Advanced Rendering Patterns
  • Green Tick Icon Setters and Getters for Styles and Class Bindings

Here’s the syntax for Array Some:

const returnValue = array.some((value, index, array) => {...}, thisArg);

Our returnValue will contain a Boolean value true or false.

As Some returns a Boolean, its result is commonly used in conditional statements.


Array Some syntax deconstructed:

See the ECMAScript Array Some specification!


In its simplest form, here is how Some behaves:

const greaterThanOne = [1, 2, 3].some(x => x > 1);
// true
console.log(greaterThanOne);

As our array contains values greater than > 1, our expression evaluates to true, and Some returns true.

Similarly, with an expression that checks if our values are greater than > 3, Some will return false:

const greaterThanThree = [1, 2, 3].some(x => x > 3);
// false
console.log(greaterThanThree);

As a performance benefit, Some will short-circuit and stop the loop on a true test result, otherwise it will continuously loop if results are false until the loop exits.

Let’s check some examples out.

Using Array Some

Here’s our data structure that we’ll be using Array Some with (take note of the additional promo property):

const items = [
  { id: '🍔', name: 'Super Burger', price: 399, promo: false },
  { id: '🍟', name: 'Jumbo Fries', price: 199, promo: false },
  { id: '🥤', name: 'Big Slurp', price: 299, promo: true}
];

Let’s use Some to check if any items are in the promo - we should expect to see our Big Slurp '🥤' trigger Some to return true:

const isInPromo = items
  .some(item => item.promo);
  
// true
console.log(isInPromo);

Using a ternary statement to calculate our total - if an item is in the promo we set the price to a flat 600 cents. Otherwise we’ll use Array Reduce to sum the price properties:

const total = isInPromo ? 600 : items.reduce((prev, next) => prev + next.price, 0);

Our example here is simple, but real enough. You can see how we’ve used the isInPromo resulting variable as part of a conditional statement - where it’s most commonly used!

Notice how simple Some is, we’re returning item.promo (either true or false) to get a final true result as one item matched our conditional test.

You can return any type of expression inside Some’s callback function (such as using comparison logic item.price > 99).

Give the live Array Some demo a try, you can toggle promo: true to promo: false and see the result change:

Bonus: Some-ing without Some

Let’s check out a for...in loop example that mimics the behaviour of Array Some:

let isInPromo = false;

for (let i = 0; i < items.length; i++) {
  const item = items[i];
  if (item.promo) {
    isInPromo = true;
    break;
  }
}

First we set isInPromo to false and it’s our job to detect when to set it to true. We’ll loop the items and if one passes, we set isInPromo to true. This is the same behaviour as Some, and if no items match then isInPromo will remain false.

Summary

You’ve now learned how to use Array Some to run a test on your array elements. If at least one element in your array, when returned as part of an expression, evaluates to true then Some will exit the loop and return true.

If no array elements pass the test Some will return false.

No array items are returned back to you, Some is exclusively for returning a Boolean result. If you do want items back, Array Map and Array Filter are better methods to use.

If you are serious about your JavaScript skills, your next step is to take a look at my JavaScript courses, they will teach you the full language, the DOM, the advanced stuff and much more!

Angular Directives In-Depth eBook Cover

Free eBook

Directives, simple right? Wrong! On the outside they look simple, but even skilled Angular devs haven’t grasped every concept in this eBook.

  • Green Tick Icon Observables and Async Pipe
  • Green Tick Icon Identity Checking and Performance
  • Green Tick Icon Web Components <ng-template> syntax
  • Green Tick Icon <ng-container> and Observable Composition
  • Green Tick Icon Advanced Rendering Patterns
  • Green Tick Icon Setters and Getters for Styles and Class Bindings

Further tips and tricks:

Thanks for reading, happy Someing!

Go to the next article in Exploring JavaScript Array Methods - Array Every!

Related blogs 🚀

Free eBooks:

Angular Directives In-Depth eBook Cover

JavaScript Array Methods eBook Cover

NestJS Build a RESTful CRUD API eBook Cover