JavaScript Array some()
Example 1
Check if any values are over 18:
const ages = [3, 10, 18, 20];
ages.some(checkAdult);
function checkAdult(age) {
return age > 18;
}
Try it Yourself »
Definition and Usage
The some()
method checks if any array elements pass a test (provided as a function).
The some()
method executes the function once for each array element:
- If the function returns true,
some()
returns true and stops. - If the function returns false,
some()
returns false and stops.
The some()
method does not execute the function for empty array elements.
The some()
method does not change the original array.
Syntax
array.some(function(value, index, arr), this)
Parameters
Parameter | Description | ||||||
function | Required. A function to run for each array element. |
||||||
Function parameters:
|
|||||||
this | Optional. Default undefined. A value passed to the function to be used as its "this" value. |
Return Value
Type | Description |
A boolean |
true if any of the aray elements pass the test, otherwise false . |
Browser Support
some()
is an ECMAScript3 (ES3) feature.
ES3 (JavaScript 1999) is fully supported in all browsers:
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | Yes | Yes | Yes | Yes | Yes |
Example 2
<p>Input: <input type="number" id="toCheck" value="15"></p>
<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>
Try it Yourself »