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: 2019-07-30T22:30:26+05:30

268 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements