Array.some

some — Executes a provided function once per array element. If all the functions returns a false value, it return false. If any returns a true value, it will break the loop and retrun true.

Description

Array.some(function callback, [object this])

Parameters

Name Description Type Default Optional
callback See callback below. function No
this object Yes
callback

The following parameters is passed to the callback.

value

The value of the current index.

index

The current index.

array

The array.

Return values

Returns false if all iterations fail, otherwise true.

Examples

Example #1 – some example
var array = [2, 4, 8]; var result = array.some(function () { console.log(this); return true; }); console.log(result); // true
var arr = [2, 4, 8]; var result = arr.some(function (value, index, array) { console.log(value, index, array); return true; }); console.log(result); // true

Passing in a custom object for this.

var array = [2, 4, 8]; var result = array.some(function () { console.log(this); return true; }, document.location); console.log(result); // true

Emulates a failed test when the value 2 is discovered. This will make it go to the next iteration.

var array = [2, 4, 8]; var result = array.some(function (value) { console.log(this); if (value === 2) { return 0; } return true; }); console.log(result); // true

All callbacks return a false value, so all iterations are performed, and the end result is set to false.

var array = [2, 4, 8]; var result = array.some(function (value) { console.log(this); return 0; }); console.log(result); // false

External references