What is the use of Array.Find() method in JavaScript?


Array.find()

Array.find() is used to return value of first element in the array that satisfies provided testing condition(user given condition).If the provided testing condition fails then array.find() returns undefined.In the following example array.find() checks whether the price elements in array are more than the given testing price(12000). If the provided testing condition true then first value that passed the test will be executed, if not undefined will be executed. 

Example

Live Demo

<html>
<body>
<p id="price"></p>
<script>
   var price = [3000, 21000, 28000, 20000, 15500];
   function checkCost(cost) {
      return cost >= 12000;
   }
   document.getElementById("price").innerHTML = price.find(checkCost);
</script>
</body>
</html>

Output

21000

Updated on: 30-Jul-2019

189 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements