JavaScript Tutorials

JavaScript Array some()
Syntax & Examples

Array.some() method

The some() method of the Array class in JavaScript returns true if at least one element in the calling array satisfies the provided testing function.


Syntax of Array.some()

There are 2 variations for the syntax of Array.some() method. They are:

1.
some(callbackFn)

Parameters

ParameterOptional/RequiredDescription
callbackFnrequiredA function to test each element in the array. It takes three arguments: element, index, and array.

This method returns true if at least one element in the calling array satisfies the provided testing function.

Returns value of type Boolean.

2.
some(callbackFn, thisArg)

Parameters

ParameterOptional/RequiredDescription
callbackFnrequiredA function to test each element in the array. It takes three arguments: element, index, and array.
thisArgoptionalA value to use as this when executing callbackFn.

This method returns true if at least one element in the calling array satisfies the provided testing function, using thisArg as the value of this inside callbackFn.

Returns value of type Boolean.



✐ Examples

1 Using some() method to check for even numbers

In JavaScript, we can use the some() method to check if there are any even numbers in an array.

For example,

  1. We define an array variable arr with elements [1, 3, 7, 8, 10].
  2. We define a callback function isEven to check if each element is an even number.
  3. We use the some() method with isEven to check if there is any even number in the array.
  4. The result is stored in the variable hasEvenNumber.
  5. We log hasEvenNumber to the console using console.log() method to see if there is any even number in the array.

JavaScript Program

const arr = [1, 3, 7, 8, 10];
const isEven = element => element % 2 === 0;
const hasEvenNumber = arr.some(isEven);
console.log(hasEvenNumber);

Output

true

2 Using some() method to check for strings with length greater than 3

We can use the some() method to check if there are any strings with a length greater than 3 in an array.

For example,

  1. We define an array variable strArr with elements ['a', 'bc', 'def', 'ghij'].
  2. We define a callback function isLengthGreaterThanThree to check if the length of each string is greater than 3.
  3. We use the some() method with isLengthGreaterThanThree to check if there is any string with a length greater than 3 in the array.
  4. The result is stored in the variable hasLongString.
  5. We log hasLongString to the console using console.log() method to see if there is any string with a length greater than 3 in the array.

JavaScript Program

const strArr = ['a', 'bc', 'def', 'ghij'];
const isLengthGreaterThanThree = element => element.length > 3;
const hasLongString = strArr.some(isLengthGreaterThanThree);
console.log(hasLongString);

Output

true

3 Using some() method with a thisArg

We can use the some() method with a thisArg to check if there are any elements in an array that satisfy a testing function using a specific context.

For example,

  1. We define an array variable arr with elements [5, 12, 8, 130, 44].
  2. We define an object context with a threshold property set to 100.
  3. We define a callback function isGreaterThanThreshold to check if each element is greater than the threshold property of the context object.
  4. We use the some() method with isGreaterThanThreshold and context as thisArg to check if there is any element greater than the threshold in the array.
  5. The result is stored in the variable hasElementGreaterThanThreshold.
  6. We log hasElementGreaterThanThreshold to the console using console.log() method to see if there is any element greater than the threshold in the array.

JavaScript Program

const arr = [5, 12, 8, 130, 44];
const context = { threshold: 100 };
function isGreaterThanThreshold(element) {
  return element > this.threshold;
}
const hasElementGreaterThanThreshold = arr.some(isGreaterThanThreshold, context);
console.log(hasElementGreaterThanThreshold);

Output

true

Summary

In this JavaScript tutorial, we learned about some() method of Array: the syntax and few working examples with output and detailed explanation for each example.