JavaScript Array some() Method

The Array.prototype.some() is a built-in JavaScript function that tests whether at least one element in an array passes the test implemented by the provided callback function. It returns a Boolean value, either true if at least one element satisfies the condition or false if none meets it.

Syntax

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

Parameters

It takes the first parameter as a function that takes the following parameters.

  1. currentValue: It is Required. The value of the current element.
  2. index: It is Optional. The array index of the current element.
  3. arr: It is Optional. The array objects the current element belongs to.

The function is the required parameter.

It takes thisValue as a second parameter, which is optional. It is the value to be passed to the function to be used as its “this” value. If the thisValue parameter is empty, the value “undefined” will be given as its “this” value.

Return Value

JavaScript some() method returns true if the callback function returns a truthy value for at least one element in the array. Otherwise, it returns false.

Example 1

let dark = [
  100,
  90,
  80,
];

console.log(dark.some(x => x > 90));

Output

true

In this example, we defined an array and then checked if the array contains a single item that satisfies the condition, and if it does, it returns true otherwise, it returns false.

The some() function executes the callback function once for each item in the array until it finds the one where the callback returns the truthy value (a value that becomes true when converted to a Boolean).

If such an item is found, the some() method immediately returns true. Otherwise, it returns false.

The callback is invoked only for indexes of an array with assigned values. It is not invoked for indexes that have been deleted or which have never been assigned values.

The callback is invoked with three arguments: 

  1. The value of the item,
  2. The index of the item,
  3. The Array object is being traversed.

If the thisArg parameter is provided to some() function, it will be used as a callback’s this value. Otherwise, the value undefined will be used as this value.

This value, ultimately observable by callback, is defined according to the usual rules for determining this seen by a function.

The some() function does not mutate the array on which it is called.

The range of elements processed by the some() method is set before the first callback invocation.

The callback will not visit items appended to the array after the call to some() begins.

If the existing, unvisited item of the array is changed by callback, its value passed to a visiting callback will be the value when some() visits that item’s index. Items that are deleted are not visited.

Example 2

To check if the specified value is present in the array, you can use the array.some() function.

const horcruxes = ['diary', 'ring', 'locket', 'nagini', 'harry'];

function remainingHorcruxes(arr, val) {
  return arr.some(function (arrVal) {
    return val === arrVal;
 });
}

console.log(remainingHorcruxes(horcruxes, 'harry'));
console.log(remainingHorcruxes(horcruxes, 'diadem'));

Output

true
false

In this example, we have defined an array and a function that checks if the passed value exists in the given array. If it exists, then it will return true otherwise returns false.

Example 3

To check if any number in the array is in the range, you can use the array.some() method.

let data = [11, 18, 19, 21, 29];

const range = {
  min: 10,
  max: 30
};

let result = data.some(function (e) {
  return e >= this.min && e <= this.max;
}, range);

console.log(result);

Output

true

We check each element against the min and max numbers in this example. If it satisfies the condition, then it returns true otherwise false.

First, we defined a range object with min and max properties.

Second, call the some() function on the marks array object and pass the callback and range object. Because we pass a range object as the second argument (thisArg), we can reference it inside the callback via this value.

That is it.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.