Array some() Method - Javascript Array

Javascript examples for Array:some

Description

The some() method checks if any of the elements pass a test against a function.

If some() finds an element, it returns true and does not check the remaining values. Otherwise it returns false

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.
currentValue Required. The value of the current element
index Optional. The array index of the current element
arrOptional. 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.

Return Value:

A Boolean. Returns true if any of the elements in the array pass the test, otherwise it returns false

The following code shows how to Check if any values in the ages array are 18 or over:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<p>Minimum age: <input type="number" id="ageToCheck" value="18"></p>

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

<p>Any ages above: <span id="demo"></span></p>

<script>
var ages = [4, 12, 16, 20];/*from  w  w w . j av  a 2 s .c o m*/

function checkAdult(age) {
    return age >= document.getElementById("ageToCheck").value;
}

function myFunction() {
    document.getElementById("demo").innerHTML = ages.some(checkAdult);
}
</script>

</body>
</html>

Related Tutorials