JavaScript Array some() Method
- Previous Page slice()
- Next Page sort()
- Go to the Previous Level JavaScript Array Reference Manual
Definition and Usage
some()
The method checks if any element in the array passes the test (as a function provided).
some()
The method executes the function once for each element in the array:
- If the function returns a truthy array element, some() returns true (and does not check the remaining values)
- Otherwise, return false
Note:some()
Does not execute the function for array elements without a value.
Note:some()
Does not change the original array.
Example
Example 1
Check if there is a value of 18 or above in the ages array:
var ages = [3, 10, 18, 20]; function checkAdult(age) { return age >= 18; } function myFunction() { document.getElementById("demo").innerHTML = ages.some(checkAdult); }
Example 2
Check if any value in the ages array is equal to or greater than a specific number:
<p>Minimum age: <input type="number" id="ageToCheck" value="18"></p> <button onclick="myFunction()">Try it</button> <p>Any ages above: <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>
Syntax
array.some(function(currentValue, index, arr) thisValue)
Parameter value
Parameter | Description | ||||||||
---|---|---|---|---|---|---|---|---|---|
function(currentValue, index, arr) | Required. The function to be run for each element in the array.
Function parameters:
|
||||||||
thisValue |
Optional. The value to be passed to the function to be used as its 'this' value. If this parameter is empty, the value 'undefined' will be passed as its 'this' value. |
Technical details
Return value: | Boolean value. Returns true if any element in the array passes the test, otherwise returns false. |
---|---|
JavaScript version: | ECMAScript 3 |
Browser support
The numbers in the table indicate the first browser version that fully supports this method.
All browsers fully support some()
Method:
Chrome | IE | Edge | Firefox | Safari | Opera |
---|---|---|---|---|---|
Chrome | IE | Edge | Firefox | Safari | Opera |
Support | 9.0 | Support | Support | Support | Support |
Related Pages
Tutorial:JavaScript Array
Tutorial:JavaScript Array Const
Tutorial:JavaScript Array Methods
Tutorial:JavaScript Sorting Arrays
Tutorial:JavaScript Array Iteration
- Previous Page slice()
- Next Page sort()
- Go to the Previous Level JavaScript Array Reference Manual