JavaScript Array some() Method
Last Updated :
12 Jul, 2024
The some() method checks if any array elements pass a test provided as a callback function, returning true if any do and false if none do. It does not execute the function for empty elements or alter the original array.
Syntax
arr.some(callback(element,index,array),thisArg);
Parameters
- callback: This parameter holds the function to be called for each element of the array.
- element: The parameter holds the value of the elements being processed currently.
- index: This parameter is optional, it holds the index of the currentValue element in the array starting from 0.
- array: This parameter is optional, it holds the complete array on which Array.every is called.
- thisArg: This parameter is optional, it holds the context to be passed as this is to be used while executing the callback function. If the context is passed, it will be used like this for each invocation of the callback function, otherwise undefined is used as default.
Return value
Returns true if any of the array elements pass the test, otherwise false.
Example: In this example the Function checkAvailability checks if a value exists in an array using some(). Function func tests checkAvailability to see if 2 and 87 exist in the array.
JavaScript
function checkAvailability(arr, val) {
return arr.some(function (arrVal) {
return val === arrVal;
});
}
function func() {
// Original function
let arr = [2, 5, 8, 1, 4];
// Checking for condition
console.log(checkAvailability(arr, 2));
console.log(checkAvailability(arr, 87));
}
func();
Example : In this example the Function isGreaterThan5 checks if any element in an array is greater than 5. Function func uses some() method to verify if any element in array satisfies condition.
JavaScript
function isGreaterThan5(element, index, array) {
return element > 5;
}
function func() {
// Original array
let array = [2, 5, 8, 1, 4];
// Checking for condition in array
let value = array.some(isGreaterThan5);
console.log(value);
}
func();
Example : In this example the Function isGreaterThan5 checks if any element in an array is greater than 5. Function func uses some() to verify if any element in array satisfies condition.
JavaScript
function isGreaterThan5(element, index, array) {
return element > 5;
}
function func() {
// Original array
let array = [-2, 5, 3, 1, 4];
// Checking for condition in the array
let value = array.some(isGreaterThan5);
console.log(value);
}
func();
We have a complete list of Javascript Array methods, to check those please go through this Javascript Array Complete reference article.
Supported Browsers:
JavaScript Array some() Method- FAQs
What does the some() method do?
The some() method tests whether at least one element in the array passes the test implemented by the provided function. It returns a Boolean value (true or false).
How does the some() method work?
The some() method executes the provided callback function once for each element present in the array until it finds one where the callback returns a truthy value. If such an element is found, some() immediately returns true. Otherwise, it returns false.
What does the some() method return?
The some() method returns true if the callback function returns a truthy value for at least one element in the array. Otherwise, it returns false
Does the some() method mutate the array?
No, the some() method does not mutate the array. It only tests elements and returns a Boolean value without modifying the original array.
Can some() be used on empty arrays?
Yes, some() can be used on empty arrays. In such cases, it returns false because there are no elements to test, and therefore no element can meet the condition.