.some() Method

  • Array Method
  • Array Methods
  • Code
  • JavaScript

Simply put, the .some() is a method that will returns the boolean true if a given callback function proves truthy for any element of an array, otherwise it returns false if all items prove falsy.

The example I like to think of is if you have a group of people and you need to figure out if someone in that group is old enough to drink.

const ages = [14, 15, 16, 17, 22, 23, 24];
const olds = ages.some((age) => age >= 21);console.log(olds);// expected output: true

Similarly, you can determine whether anyone in that group is exactly a certain age.

const ages = [14, 15, 16, 17, 22, 23, 24];
const olds = ages.some((age) => age == 21);console.log(olds);// expected output: false

It's important to note that if the condition proves Truthy for the callback, then .some() will return true. Truthiness is a weird concept in JavaScript, but my understanding is that it's a way that the language will use type coercion to return a boolean on a comparison that isn't a boolean in the first place. Basically it's not a true boolean, but still returns true.

From MDN, the following are examples of truthy values:

if (true)if ({})if ([])if (42)if ("0")if (new Date())if (-42)if (12n)if (3.14)if (-3.14)if (Infinity)if (-Infinity)

One last important note about .some() is that unlike previous array methods I've written about, .some() will return true as soon as it finds an element in the array that returns truthy for the given callback. As such it doesn't necessarily check or run against each item in an array, and will escape the method and return true when it first encounters a value that meets the condition.

From MDN: The .some() method checks if any of the elements in an array pass a test (provided as a function).

See more examples and documentation here.