JavaScript Array some()

Example 1

Check if any values are over 18:

const ages = [3, 10, 18, 20];

ages.some(checkAdult);
function checkAdult(age) {
  return age > 18;
}
Try it Yourself »

Description

The some() method checks if any array elements pass a test (provided as a callback function).

The some() method executes the callback function once for each array element.

The some() method returns true (and stops) if the function returns true for one of the array elements.

The some() method returns false if the function returns false for all of the array elements.

The some() method does not execute the function for empty array elements.

The some() method does not change the original array.


Syntax

array.some(function(value, index, arr), this)

Parameters

Parameter Description
function Required.
A function to run for each array element.
Function parameters:
value Required.
The value of the current element.
index Optional.
The index of the current element.
arr Optional.
The array the current element belongs to.
this Optional. Default undefined.
A value passed to the function to be used as its "this" value.

Return Value

Type Description
A boolean true if any of the aray elements pass the test, otherwise false.


Example 2

<p>Input: <input type="number" id="toCheck" value="15"></p>

<button onclick="myFunction()">Test</button>

<p>Values higher: <span id="demo"></span></p>

<script>
const numbers = [4, 12, 16, 20];

function checkValue(x) {
  return x > document.getElementById("toCheck").value;
}

function myFunction() {
  document.getElementById("demo").innerHTML = numbers.some(checkValue);
}
</script>
Try it Yourself »

Browser Support

some() is an ECMAScript3 (ES3) feature.

ES3 (JavaScript 1999) is fully supported in all browsers:

Chrome Edge Firefox Safari Opera IE
Yes Yes Yes Yes Yes Yes


Copyright 1999-2023 by Refsnes Data. All Rights Reserved.