JavaScript Array some() Method

Array 对象参考手册JavaScript Array Object

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);
}

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:
Parameter Description
currentValue Required. The value of the current element.
index Optional. The index of the current element.
arr Optional. The array object the current element belongs to.
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>

Try it Yourself »

Array 对象参考手册JavaScript Array Object

Other Extensions