JavaScriptArrays

Array.prototype.some()

Checks whether at least one element matches a condition.

Returns
true if any element passed the test, otherwise false.
Changes the original
No
On this page

Example

#
JavaScript
const ages = [12, 15, 19, 14]

console.log(ages.some((age) => age >= 18))
console.log(ages.some((age) => age > 30))
console.log([].some(() => true))
Console
true
false
false

Definition and usage

#

The some() method returns true if the testing function returns a truthy value for at least one element. It stops checking at the first match.

It’s a clear way to ask whether there’s any match at all: a discounted product in the cart, a student with an overdue assignment, a form with even one empty field.

For an empty array, some() always returns false, because there’s no element that could satisfy the condition.

Syntax

#
Syntax
array.some(callbackFn)
array.some(callbackFn, thisArg)

Parameters

#
  • callbackFn

    function (element, index, array)

    The testing function.
  • thisArg

    optional

    The this value inside the function.

Browser support

#

Widely available

Works in all modern browsers, phones included. You can use it without worry.

  • Chrome
  • Edge
  • Firefox
  • Safari

Good practices

#
  • To check whether an array contains a specific value, includes() is simpler.
  • some() stops at the first match, so it’s more efficient than filter(...).length > 0.

Related entries

#

Spotted a mistake or missing an example? Write to us.