Skip to main content

some() Method – How to Check If Some Elements Pass a Test

Whenever you use some() on a JavaScript array, the method does the following:

  1. It checks if some elements in the calling array pass its function argument's test.
  2. It returns true if at least one of the array's items passes the test. Otherwise, it returns false.
note
  • some() does not alter the original array.
  • A calling array is an array on which you used some(). So, in bestColorsList.some(func), bestColorsList is the calling array.
  • some() is sometimes written as Array.prototype.some() because it is a method of the Array object's prototype property.

Syntax of the some() Method

some() accepts two arguments: a "callback function" and a "thisValue." Here is the syntax:

callingArray.some(callback, thisValue);

Argument 1: callback

A function is the first argument accepted by the some() method. It is a required argument containing the code you want the computer to use to test each item of the calling array.

The callback function returns the Boolean true (or false).

In other words, the callback returns true if some of the calling array's items pass the specified test.

But the callback will return false if all the items fail the test.

some()'s function argument accepts three parameters: currentItem, index, and an array.

callingArray.some((currentItem, index, array) => {
// Test to run on each item of the calling array
});
note

() => {...} is an arrow function shorthand for function () {...}.

Parameter 1: currentItem

The currentItem parameter is required. It represents the current calling array's item the computer is currently processing.

Parameter 2: index

The index parameter is optional. It represents the index number of the item the computer is currently processing.

Parameter 3: array

The array parameter is also optional. It represents the calling array.

note
  • You can rename the currentItem, index, and array parameters to anything you prefer.
  • The computer will invoke some()'s function argument once for each item inside the calling array until it finds a truthy value. Once some() discovers a truthy element, it will immediately return true and stop its iteration through the calling array. The invocation will be in the order of the array items from left to right.
  • some() does not change the original array. (Although you can do so with the callback argument.)

Argument 2: thisValue

A thisValue is the second argument accepted by the some() method. It is an optional argument representing the value you want to use as the function argument's this value.

callingArray.some((currentItem, index, array) => {}, thisValue);

Suppose you do not provide a second argument. In that case, the computer will use undefined as the callback function's this value.

Example 1: How to Use some() without a thisValue Argument

Here is an example of the some() method without a thisValue argument:

const colorsArray = ["red", "blue", "yellow", "green", "white", "pink"];

function itemLength(currentItem) {
return currentItem.length > 5;
}

const trueOrFalse = colorsArray.some(itemLength);
console.log(trueOrFalse);

// The console.log invocation above will return: true

Try it on StackBlitz

In the snippet above, we used the some() method to confirm whether at least one item exists in the colorsArray having a length greater than five (5).

Therefore, the browser returned true because the array has one item ("yellow") whose length is greater than five (5).

In other words, some elements exist in the colorsArray having a length greater than five (5).

note

The colorsArray is the calling array in the example above.

Example 2: How to Use some() with a thisValue Argument

Here is an example of the some() method with a thisValue argument:

const colorsArray = ["red", "blue", "yellow", "green", "white", "pink"];
const thisValueArray = ["Current Test Item: ", "Next Test Item: "];

function itemLength(currentItem, ind, arr) {
console.log(this[0] + currentItem);
console.log(this[1] + arr[ind + 1]);
return currentItem.length > 5;
}

const trueOrFalse = colorsArray.some(itemLength, thisValueArray);
console.log(trueOrFalse);

// The console.log invocation above will return:
// "Current Test Item: red"
// "Next Test Item: blue"
// "Current Test Item: blue"
// "Next Test Item: yellow"
// "Current Test Item: yellow"
// "Next Test Item: green"
// true

Try it on StackBlitz

In the snippet above, we used the some() method to confirm if some items exist in the colorsArray having a length greater than five (5).

We also used the thisValueArray as the method's this value.

tip

Use the every() method to check whether every element in the array passes the function argument's test.

Important Stuff to Know about the some() Method

The some() method's callback function does not execute for empty array slots. For instance, consider the following code:

const colorsArray = ["red", "blue", , "green", "white", "pink"];

function itemUndefined(currentItem) {
return currentItem.length === undefined;
}

const trueOrFalse = colorsArray.some(itemUndefined);
console.log(trueOrFalse);

// The console.log invocation above will return: false

Try it on StackBlitz

The snippet above returned false because some() did not invoke its callback argument for the empty slot.

Overview

some() returns true if some of its calling array's items pass its function argument's test. Otherwise, it returns false.

Your support matters: Buy me a coffee to support CodeSweetly's mission of simplifying coding concepts.

Join CodeSweetly Newsletter