- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
JavaScript Array find() function
The find() method of JavaScript is used to return the first elements value in an array, if the condition is passed, otherwise the return value is undefined. The syntax is as follows −
array.find(function(val, index, arr),thisValue)
Here, function is a function with val, which is the value of the current element. The index is the array index, and arr is the array. The this value parameter is the value to be passed to the function.
Example
<!DOCTYPE html> <html> <body> <h2>Ranking Points</h2> <p>Get the points (first element) 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.find(pointsFunc); } </script> </body> </html>
Output
Now, click on the “Result” button −
Let us see another example, wherein the result would be undefined −
Example
<!DOCTYPE html> <html> <body> <h2>Ranking Points</h2> <p>Get the points (first element) above 400...</p> <button onclick="display()">Result</button> <p id="demo"></p> <script> var pointsArr = [50, 100, 200, 300, 400]; function pointsFunc(points) { return points > 400; } function display() { document.getElementById("demo").innerHTML = pointsArr.find(pointsFunc); } </script> </body> </html>
Output
Now, click on the “Result” button −
- Related Articles
- JavaScript Array fill() function
- JavaScript Array findIndex() function
- JavaScript Array some() function
- Sorting Array with JavaScript reduce function - JavaScript
- Array findIndex() function in JavaScript
- Array some() function in JavaScript
- divisibleBy() function over array in JavaScript
- Currified function that multiples array elements in JavaScript
- Which algorithm does the JavaScript Array#sort() function use?
- Accessing an array returned by a function in JavaScript
- Function to find out palindrome strings JavaScript
- Find Max Slice Of Array | JavaScript
- Find average of each array within an array JavaScript
- How to pass a PHP array to a JavaScript function?
- Find average of each array within an array in JavaScript

Advertisements