Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

Array.prototype.some()

BaselineWidely available

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

js
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:

element

The current element being processed in the array.

index

The index of the current element being processed in the array.

array

The arraysome() was called upon.

thisArgOptional

A value to use asthis 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 causecallbackFn to be invoked on them again.
  • If an existing, yet-unvisited element of the array is changed bycallbackFn, 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.

js
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.

js
[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:

js
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

js
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.

js
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.

js
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.

js
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

Browser compatibility

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp