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

JavascriptObject Oriented ProgrammingProgramming

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
raja
Published on 28-May-2019 09:54:09
Advertisements