JavaScript Array some() function



The some() method of JavaScript is used to check if any of the elements in an array fulfills the condition.

The syntax is as follows −

array.some(function(currentVal, index, arr), val)

Above, the parameters under function(), include currentVal – value of the current element, index – array index, whereas val the array object the current element fits

Let us now implement the some() method in JavaScript −

Example

 Live Demo

<!DOCTYPE html>
<html>
<body>
   <h2>Ranking Points</h2>
   <p>Is any point above 400...</p>
   <button onclick="display()">Result</button>
   <p id="demo"></p>
   <script>
      var pointsArr = [50, 100, 200, 300, 400, 500, 600];
      function pointsFunc(points) {
         return points > 400;
      }
      function display() {
         document.getElementById("demo").innerHTML = pointsArr.some(pointsFunc);
      }
   </script>
</body></html>

Output

Click the “Result” button −

Example

 Live Demo

<!DOCTYPE html>
<html>
<body>
   <h2>Ranking Points</h2>
   <p>Is any point equal to 550...</p>
   <button onclick="display()">Result</button>
   <p id="demo"></p>
   <script>
      var pointsArr = [10, 20, 30, 40, 50, 60, 70, 80, 90, 1000];
      function pointsFunc(points) {
         return points == 550;
      }
      function display() {
         document.getElementById("demo").innerHTML = pointsArr.some(pointsFunc);
      }
   </script>
</body></html>

Output

Click the “Result” button −


Advertisements