Array.prototype.find() method in JavaScript.


The Array.prototype.find() method returns the first element value that satisfy a given condition in an array.

Following is the code for the Array.prototype.find() method −

Example

 Live Demo

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
   body {
      font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
   }
   .find {
      font-size: 20px;
      font-weight: 500;
   }
</style>
</head>
<body>
<h1>JavaScript Array Find() example</h1>
<div class="find"></div>
<button class="fillArr">CLICK HERE</button>
<h3>Click on the above button to find lion among the animals</h3>
<script>
   function findLion(animal) {
      return animal === "lion";
   }
   let fillEle = document.querySelector(".find");
   let arr = ["cow", "lion", "bull", "tiger"];
   fillEle.innerHTML = arr;
   document.querySelector(".fillArr").addEventListener("click", () => {
      fillEle.innerHTML = arr.find(findLion);
   });
</script>
</body>
</html>

Output

The above code will produce the following output −

On clicking the “CLICK HERE” button −

Updated on: 16-Jul-2020

88 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements