JavaScript Array some()
Example
Check if any values in the ages array are 18 or over:
const ages = [3, 10, 18, 20];
ages.some(checkAdult) // Returns true
function checkAdult(age) {
return age >= 18;
}
Try it Yourself »
More "Try it Yourself" examples below.
Definition and Usage
The some() method checks if any of the elements in an array pass a test (provided as a function).
some() executes the function once for each element in the array:
- If it finds an array element where the function returns a true value, some() returns true (and does not check the remaining values)
- Otherwise it returns false
some() does not execute the function for empty array elements.
some() does not change the original array.
Browser Support
some() is fully supported in all modern browsers:
| Chrome | IE | Edge | Firefox | Safari | Opera |
| Yes | 9.0 | Yes | Yes | Yes | Yes |
Syntax
array.some(function(currentValue, index, arr), thisValue)
Parameter Values
| Parameter | Description | ||||||||
|---|---|---|---|---|---|---|---|---|---|
| function(currentValue, index, arr) | Required. A function to be run for each element in the array. Function arguments:
|
||||||||
| thisValue | Optional. A value to be passed to the function to be used as its "this" value. If this parameter is empty, the value "undefined" will be passed as its "this" value |
Technical Details
| Return Value: | A Boolean. Returns true if any of the elements in the array pass the test, otherwise it returns false |
|---|---|
| JavaScript Version: | ECMAScript 3 |
More Examples
Example
Check if any of the values in the ages array are a specific number or over:
<p>Minimum age: <input type="number" id="ageToCheck" value="18"></p>
<button onclick="myFunction()">Try it</button>
<p>Any ages above: <span id="demo"></span></p>
<script>
const = [4, 12, 16, 20];
function checkAdult(age) {
return age >= document.getElementById("ageToCheck").value;
}
function myFunction() {
document.getElementById("demo").innerHTML = ages.some(checkAdult);
}
</script>
Try it Yourself »

