The some method conducts a test to determine whether at least one element within the array meets the criteria defined by the supplied function. If any element satisfies the condition, it will return true; otherwise, it returns false.
Note: If some method is applied on an empty array, it returns false always.
Syntax
array.some(callback_funct(element, index, array), thisArg);
Parameter
callback_funct: It is the function that tests each element present in the array. It undertakes the following three arguments:
- element: It is the current element, being in process.
- index: Although it is optional, it is the index value of the current element in process.
- arr: It is the given array on which some method performs the test.
thisArg: This is an optional argument that serves as the 'this' context when the callback function is invoked. In the absence of a provided value, 'undefined' will be utilized as the 'this' context.
Return
It provides a boolean outcome. If an element is identified that returns a true value to the callback function, it will yield true. If not, it will return false.
Note: The array.some does not affect the original array.
Points to note:
- Before the first calling of function, it is required to set the range of the elements to implement some method.
- The callback function will not visit those elements added after invoking some method.
- It does not work on the deleted array elements.
- If the callback function changes an existing and unchanged element, the value gets defined to the array.
JavaScript Array some Method Example
Let us explore a few examples to gain a clearer understanding:
Example1
Below is a straightforward example demonstrating the use of the some method.
<html>
<head> <h5> JavaScript Array Methods </h5> </head>
<body>
<script>
var arr=[12,81,23,34];
function test(arr)
{
return(arr>80);
} // test() will return a true value.
var ajen=arr.some(test);
document.write("Its "+ajen);
</script>
</body>
</html>
Output:
It evaluates if there exists any element that exceeds the value of 80. Consequently, at least one element meets the specified criterion. Therefore, the some function yields True.
Example2
Verifying whether any element within an array matches another element.
<html>
<head> <h5> JavaScript Array Methods </h5> </head>
<body>
<script>
var arr=['John','Tom','James','Sheero']; // An array is defined
function ismatching(arr)
{
return arr=='Sheero';
} //It matches if any element in the array is equal to Sheero.
var chk=arr.some(ismatching);
document.write("Yes, "+chk+". One match found.");
</script>
</body>
</html>
Output:
Example3
Checking whether any array element is found even.
<html>
<head> <h5> JavaScript Array Methods </h5> </head>
<body>
<script>
var arr=[2,3,4,6,9]; //Defining array elements
function check(arr)
{
return arr%2==0;
} //This function checks if any element is even or not.
var test=arr.some(check);
document.write("Yes, "+test);
</script>
</body>
</html>
Output:
Example4
The aforementioned example can also be executed utilizing an Arrow function.
<html>
<head> <h5> JavaScript Array Methods </h5> </head>
<body>
<script>
var arr=[2,3,4,6,9]; //Defining array elements
var check=(element)=>element%2===0;
document.write("Yes found. So, it should be ");
document.write(arr.some(check));
</script>
</body>
</html>
Output: