• JavaScript Video Tutorials

JavaScript - Array some() Method



In JavaScript, the Array.some() method takes a callback function and verifies whether at least one element in the array passes the test provided by the callback function. If at least one element passes the test, it returns "true". It returns "false", if the function returns false for all of the array elements.

The some() method does not execute the function for empty array elements. It does not change the original array; it only checks the elements for the specified condition.

Syntax

Following is the syntax of JavaScript Array.some() method −

some(callbackFn, thisArg)

Parameters

This method accepts two parameters. The same is described below −

  • callbackFn − A function that will be called for each element in the array. This function can take up to three arguments:
    • currentValue − The current element being processed in the array.
    • index (optional) − The index of the current element being processed in the array.
    • array (optional) − The array some() was called upon.
  • thisArg (optional) − A value to use as this when executing the callback function.

Return value

This method returns a Boolean value as a result.

Examples

Example 1

In the following example, we are checking for atleast one positive number in the provided array elements using the JavaScript Array.some() method.

<html>
<body>
   <script>
      let numbers = [2, 4, -1, 6, 8];
      let isPositive = numbers.some(function(num) {
         return num > 0;
      });
      document.write(isPositive);
   </script>
</body>
</html>

After executing the above program, the some() method returns "true" because at least one element (2) in the numbers array is positive.

Output

true

Example 2

In this example, we are checking for atleast one string element that has a lenght of '5'.

<html>
<body>
   <script>
      let words = ["apple", "banana", "cherry", "date"];
      let isLengthFive = words.some(function(word) {
         return word.length === 5;
      });
      document.write(isLengthFive);
   </script>
</body>
</html>

After executing the above program, the some() method returns "true" because at least one element (orange) in the array that has a length of 5.

Output

true

Example 3

If not atleast one element in the array passes the condition implemented by the provided function, the some() method returns "false" as result.

<html>
<body>
   <script>
      let numbers = [1, 3, 5, 9, 7];
      let isEven = numbers.some(function(num) {
         return num % 2 === 0;
      });
      document.write(isEven);
   </script>
</body>
</html>

Output

false

Example 4

In the example below, we are checking whether a particular element exists in an array.

<html>
<body>
   <script>
      const fruits = ["apple", "banana", "cherry", "mango"];

      function exists(arr, val) {
         return arr.some((arrVal) => val === arrVal);
      }
      document.write(exists(fruits, "orange"), "<br>");
      document.write(exists(fruits, "banana"));
   </script>
</body>
</html>

Output

false
true
Advertisements