The some() method tests whether at least one element in the array passes the test implemented by the provided callback function. It returns a Boolean value.
The syntax of Array.some() method is:
some(callbackfn: function, thisArg?: any): boolean
This some() method of Array determines whether the specified callback function returns true for any element of an array.
Parameter | Optional/Required | Description |
---|---|---|
callbackfn | required | A function to test for each element, taking three arguments: the current element, the index of the current element, and the array being traversed. |
thisArg | optional | Value to use as this when executing callbackfn. |
Array.some() returns value of type boolean
.
The some() method can be used to check if any elements in the array are greater than a specified number.
For example,
arr
with numeric values [1, 2, 3, 4, 5].some(callbackfn)
to test if any elements are greater than 3.result
.result
to the console.const arr = [1, 2, 3, 4, 5];
const result = arr.some(element => element > 3);
console.log(result);
true
The some() method can be used to check if any elements in the array are even.
For example,
arr
with numeric values [1, 3, 5, 7, 8].some(callbackfn)
to test if any elements are even.result
.result
to the console.const arr = [1, 3, 5, 7, 8];
const result = arr.some(element => element % 2 === 0);
console.log(result);
true
The some() method can be used to check if a specific element exists in the array.
For example,
arr
with string values ['apple', 'banana', 'cherry'].some(callbackfn)
to test if 'banana' is present in the array.result
.result
to the console.const arr = ['apple', 'banana', 'cherry'];
const result = arr.some(element => element === 'banana');
console.log(result);
true
In this TypeScript tutorial, we learned about some() method of Array: the syntax and few working examples with output and detailed explanation for each example.