Array.prototype.some()
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
The some()
method of Array
instances tests whether
at least one element in the array passes the test implemented by the provided
function. 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
Syntax
some(callbackFn)
some(callbackFn, thisArg)
Parameters
callbackFn
-
A function to execute for each element in the array. It should return a truthy value to indicate the element passes the test, and a falsy value otherwise. The function is called with the following arguments:
thisArg
Optional-
A value to use as
this
when executingcallbackFn
. See iterative methods.
Return value
false
unless callbackFn
returns a truthy value for an array element, in which case true
is immediately returned.
Description
The some()
method is an iterative method. It calls a provided callbackFn
function once for each element in an array, until the callbackFn
returns a truthy value. If such an element is found, some()
immediately returns true
and stops iterating through the array. Otherwise, if callbackFn
returns a falsy value for all elements, some()
returns false
. Read the iterative 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 returns false
for any condition.
callbackFn
is invoked only for array indexes which have assigned values. It is not invoked for empty slots in sparse arrays.
some()
does not mutate the array on which it is called, but the function provided as callbackFn
can. Note, however, that the length of the array is saved before the first invocation of callbackFn
. 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).
The some()
method is generic. It only expects the this
value to have a length
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 functions provide 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 the includes()
method, this custom function returns true
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"); // false
checkAvailability(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); // false
getBoolean("false"); // false
getBoolean(1); // true
getBoolean("true"); // true
Using the third argument of callbackFn
The array
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 uses filter()
to extract the positive values and then uses some()
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)); // false
console.log([1, , 1].some((x) => x !== 1)); // false
console.log([1, undefined, 1].some((x) => x !== 1)); // true
Calling some() on non-array objects
The some()
method reads the length
property of this
and then accesses each property whose key is a nonnegative integer less than length
until they all have been accessed or callbackFn
returns true
.
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 Language Specification # sec-array.prototype.some |
Browser compatibility
BCD tables only load in the browser