JavaScript Array some()
Example 1
Check if any values are over 18:
ages.some(checkAdult);
function checkAdult(age) {
return age > 18;
}
Description
The some()
method checks if any array elements pass a test (provided as a callback function).
The some()
method executes the callback function once for each array element.
The some()
method returns true
(and stops) if the function returns
true
for one of the array elements.
The some()
method returns false
if the function returns
false
for all of the array elements.
The some()
method does not execute the function for empty array elements.
The some()
method does not change the original array.
Syntax
Parameters
A function to run for each array element.
The value of the current element.
The index of the current element.
The array the current element belongs to.
A value passed to the function to be used as its "this" value.
Return Value
true
if any of the array elements pass the test, otherwise false
.
Example 2
<button onclick="myFunction()">Test</button>
<p>Values higher: <span id="demo"></span></p>
<script>
const numbers = [4, 12, 16, 20];
function checkValue(x) {
return x > document.getElementById("toCheck").value;
}
function myFunction() {
document.getElementById("demo").innerHTML = numbers.some(checkValue);
}
</script>
Array Tutorials:
Browser Support
some()
is an ECMAScript3 (JavaScript 1999) feature.
It is supported in all browsers:
Chrome | Edge | Firefox | Safari | Opera | IE |
Yes | Yes | Yes | Yes | Yes | Yes |