JavaScript Array some() Method
Example
Check if any element in the array is greater than 18:
var ages = [3, 10, 18, 20];
function checkAdult(age) {
return age >= 18;
}
function myFunction() {
document.getElementById("demo").innerHTML = ages.some(checkAdult);
}
function checkAdult(age) {
return age >= 18;
}
function myFunction() {
document.getElementById("demo").innerHTML = ages.some(checkAdult);
}
The output result is:
true
Try it Yourself »
Definition and Usage
The some() method is used to check whether elements in an array satisfy a specified condition (provided by a function).
The some() method executes each element of the array in sequence:
- If an element satisfies the condition, the expression returnstrue, and the remaining elements will not be checked.
- If there are no elements that satisfy the condition, it returns false.
Note:some() does not execute on empty arrays.
Note:some() does not change the original array.
Browser Support
The numbers in the table specify the first browser version that fully supports the method.
| Method | |||||
|---|---|---|---|---|---|
| some() | Yes | 9 | 1.5 | Yes | Yes |
Syntax
array.some(function(currentValue,index,arr),thisValue)
Parameter Description
| Parameter | Description | ||||||||
|---|---|---|---|---|---|---|---|---|---|
| function(currentValue, index,arr) | Required. A function that will be executed for each element in the array. Function parameters:
|
||||||||
| thisValue | Optional. An object to use as "this" when executing the callback, passed to the function. If thisValue is omitted, the value of "this" is "undefined". |
Technical Details
| Return value: | A Boolean. Returns true if any element in the array satisfies the condition; otherwise, returns false. |
|---|---|
| JavaScript Version: | 1.6 |
More Examples
Example
Check if the arrayageshas any element greater than the value entered in the input field:
<p>Minimum age: <input type="number" id="ageToCheck" value="18"></p>
<button onclick="myFunction()">Click me</button>
<p>Result: <span id="demo"></span></p>
<script>
var ages = [4, 12, 16, 20];
function checkAdult(age) {
return age >= document.getElementById("ageToCheck").value;
}
function myFunction() {
document.getElementById("demo").innerHTML = ages.some(checkAdult);
}
</script>
<button onclick="myFunction()">Click me</button>
<p>Result: <span id="demo"></span></p>
<script>
var ages = [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 »
JavaScript Array Object