Array.prototype.some()
BaselineWidely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
Thesome()
method ofArray
instances tests whetherat least one element in the array passes the test implemented by the providedfunction. It returns true if, in the array, it finds an element for which the provided function returns true; otherwise it returns false. It doesn't modify the array.
Try it
const array = [1, 2, 3, 4, 5];// Checks whether an element is evenconst even = (element) => element % 2 === 0;console.log(array.some(even));// Expected output: true
Syntax
some(callbackFn)some(callbackFn, thisArg)
Parameters
callbackFn
A function to execute for each element in the array. It should return atruthy value to indicate the element passes the test, and afalsy value otherwise. The function is called with the following arguments:
thisArg
OptionalA value to use as
this
when executingcallbackFn
. Seeiterative methods.
Return value
false
unlesscallbackFn
returns atruthy value for an array element, in which casetrue
is immediately returned.
Description
Thesome()
method is aniterative method. It calls a providedcallbackFn
function once for each element in an array, until thecallbackFn
returns atruthy value. If such an element is found,some()
immediately returnstrue
and stops iterating through the array. Otherwise, ifcallbackFn
returns afalsy value for all elements,some()
returnsfalse
. Read theiterative methods section for more information about how these methods work in general.
some()
acts like the "there exists" quantifier in mathematics. In particular, for an empty array, it returnsfalse
for any condition.
callbackFn
is invoked only for array indexes which have assigned values. It is not invoked for empty slots insparse arrays.
some()
does not mutate the array on which it is called, but the function provided ascallbackFn
can. Note, however, that the length of the array is savedbefore the first invocation ofcallbackFn
. Therefore:
callbackFn
will not visit any elements added beyond the array's initial length when the call tosome()
began.- Changes to already-visited indexes do not cause
callbackFn
to be invoked on them again. - If an existing, yet-unvisited element of the array is changed by
callbackFn
, its value passed to thecallbackFn
will be the value at the time that element gets visited.Deleted elements are not visited.
Warning:Concurrent modifications of the kind described above frequently lead to hard-to-understand code and are generally to be avoided (except in special cases).
Thesome()
method isgeneric. It only expects thethis
value to have alength
property and integer-keyed properties.
Examples
Testing value of array elements
The following example tests whether any element in the array is bigger than 10.
function isBiggerThan10(element, index, array) { return element > 10;}[2, 5, 8, 1, 4].some(isBiggerThan10); // false[12, 5, 8, 1, 4].some(isBiggerThan10); // true
Testing array elements using arrow functions
Arrow functionsprovide a shorter syntax for the same test.
[2, 5, 8, 1, 4].some((x) => x > 10); // false[12, 5, 8, 1, 4].some((x) => x > 10); // true
Checking whether a value exists in an array
To mimic the function of theincludes()
method, this custom function returnstrue
if the element exists in the array:
const fruits = ["apple", "banana", "mango", "guava"];function checkAvailability(arr, val) { return arr.some((arrVal) => val === arrVal);}checkAvailability(fruits, "grapefruit"); // falsecheckAvailability(fruits, "banana"); // true
Converting any value to Boolean
const TRUTHY_VALUES = [true, "true", 1];function getBoolean(value) { if (typeof value === "string") { value = value.toLowerCase().trim(); } return TRUTHY_VALUES.some((t) => t === value);}getBoolean(false); // falsegetBoolean("false"); // falsegetBoolean(1); // truegetBoolean("true"); // true
Using the third argument of callbackFn
Thearray
argument is useful if you want to access another element in the array, especially when you don't have an existing variable that refers to the array. The following example first usesfilter()
to extract the positive values and then usessome()
to check whether the array is strictly increasing.
const numbers = [3, -1, 1, 4, 1, 5];const isIncreasing = !numbers .filter((num) => num > 0) .some((num, idx, arr) => { // Without the arr argument, there's no way to easily access the // intermediate array without saving it to a variable. if (idx === 0) return false; return num <= arr[idx - 1]; });console.log(isIncreasing); // false
Using some() on sparse arrays
some()
will not run its predicate on empty slots.
console.log([1, , 3].some((x) => x === undefined)); // falseconsole.log([1, , 1].some((x) => x !== 1)); // falseconsole.log([1, undefined, 1].some((x) => x !== 1)); // true
Calling some() on non-array objects
Thesome()
method reads thelength
property ofthis
and then accesses each property whose key is a nonnegative integer less thanlength
until they all have been accessed orcallbackFn
returnstrue
.
const arrayLike = { length: 3, 0: "a", 1: "b", 2: "c", 3: 3, // ignored by some() since length is 3};console.log(Array.prototype.some.call(arrayLike, (x) => typeof x === "number"));// false
Specifications
Specification |
---|
ECMAScript® 2026 Language Specification # sec-array.prototype.some |