Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

JavaScript Array some() Method

JavaScript Array some() Method

Array Object Reference JavaScript Array object

Example

Check whether 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 »


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 turn:

Note: some() does not test empty arrays.

Note: some() does not change the original array.


Browser support

The numbers in the table indicate the first browser version that supports this method.

Method
some() Yes 9 1.5 Yes Yes

Syntax


array.some(function(currentValue,index,arr),thisValue)

Parameter Description

Parameters Description
function(currentValue, index,arr) Required. A function that is executed for each element in the array
 函数参数:       
   
Parameter Description   currentValue Required. The value of the current element   index Optional. The index of the current element   arr Required. The array object to which the current element belongs | Parameter | Description | *currentValue* | Required. The value of the current element | *index* | Optional. The index of the current element | *arr* | Optional. The array object to which the current element belongs | | Parameter | Description | | *currentValue* | Required. The value of the current element | | *index* | Optional. The index of the current element | | *arr* | Optional. The array object to which the current element belongs | | *thisValue* | Optional. An object passed to the function as the value of "this" when the callback is executed. If thisValue is omitted, the value of "this" is "undefined" |

Technical Details

Return Value: Boolean value. If there is an element in the array that satisfies the condition, it returns true; otherwise it returns false.
JavaScript version: 1.6

More examples

Example

Check the array ages whether there is an element greater than the value entered in the input box:

Minimum age: "number"

id=

"ageToCheck"

value=

"18"