Javascript - Array some() Method

The some() method checks if any of the elements in an array pass a test.

Description

The some() method checks if any of the elements in an array pass a test.

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

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

Otherwise it returns false

The some() does not run the function for array elements without values.

And it does not change the original array.

Syntax

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

Parameter Values

Parameter Require Description
function(currentValue, index, arr) Required. A function to be run for each element in the array.
thisValue Optional. A value to be used as its "this" value. If this parameter is empty, the value "undefined" will be used

function(currentValue, index, arr)

Argument Require Description
currentValue Required. The value of the current element
index Optional. The array index of the current element
arr Optional. The array object the current element belongs to

Return

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

Example

Check if any values in the ages array are 18 or over:

Demo

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

function checkAdult(age) {
    return age >= 18;
}

console.log( ages.some(checkAdult));//from   www  .  jav a  2s  .co  m

Result