JavaScript Array some() Method

JavaScript Array some() Method performs testing and checks if at least a single array element passes the test, implemented by the provided function.

If the test is passed, it returns true. Else, returns false.

The some() method does not execute the function for empty array elements.

If some() method is applied on an empty array, it returns false always.

The some() method does not change the original array.

Syntax

array.some(CallbackFunction(element, index, arr), this)

Parameters

CallbackFunction: It is the function that tests each element present in the array. It undertakes the following three arguments:

  • element: Required. It is the current element, being in process.
  • index: Optional. it is the index value of the current element in process.
  • arr: Optional. It is the given array on which some() method performs the test.

thisArg: It is an optional parameter, used as ‘this’ value while executing the callback function. If we do not provide, ‘undefined’ will be used as ‘this’ value.

Return Value

It returns a boolean value. true if any of the aray elements pass the test, otherwise false.

Example

const array = [1, 2, 3, 4, 5];

// checks whether an element is even
const even = (element) => element % 2 === 0;

console.log(array.some(even));
// expected output: true
function isBiggerThan10(element, index, array) {
  return element > 10;
}

[2, 5, 8, 1, 4].some(isBiggerThan10);  // false
[12, 5, 8, 1, 4].some(isBiggerThan10); // true

This method is a JavaScript extension to the ECMA-262 standard; as such it may not be present in other implementations of the standard.