Easy Tutorial
❮ Dom Obj Body Event Onfocusin ❯

JavaScript Array some() Method

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

Output result:

true

Definition and Usage

The some() method tests whether at least one element in the array passes the test implemented by the provided function.

The some() method executes the function once for each element present in the array:

Note: some() does not execute the function for array elements without values.

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 Chrome IE Firefox Safari Opera
some() Yes 9 1.5 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. <br>Function arguments: <br>currentValue - Required. The value of the current element. <br>index - Optional. The array index of the current element. <br>arr - Optional. The array object the current element belongs to.
thisValue Optional. A value to be passed to the function to be used as its "this" value. <br>If this parameter is empty, the value "undefined" will be passed as its "this" value.

Technical Details

Return Value: Boolean. Returns true if any of the elements in the array pass the test, otherwise it returns false.
JavaScript Version: 1.6

More Examples

Example

Check if any element in the ages array is 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>

JavaScript Array Object

❮ Dom Obj Body Event Onfocusin ❯