

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 findIndex() function
The findIndex() method of JavaScript is used to return the index of the first element in an array, if the condition is passed.
The syntax is as follows −
array.findIndex(function(currentValue, index, arr), thisValue)
Let us now implement the findIndex() method in JavaScript −
Example
<!DOCTYPE html> <html> <body> <h2>Rank</h2> <button onclick="display()">Result</button> <p id="demo"></p> <p>Finding the index of the player with highest rank.</p> <script> var points = [100, 150, 200, 250, 300, 400]; function topRank(points) { return points >= 400; } function display() { document.getElementById("demo").innerHTML = "index = "+points.findIndex(topRank); } </script> </body>
Output
Click “Result” to get the index −
Example
<!DOCTYPE html> <html> <body> <h2>Rank</h2> <button onclick="display()">Result</button> <p id="demo"></p> <p>Finding the index of the player with specific rank points.</p> <script> var points = [100, 150, 200, 250, 300, 400]; function topRank(points) { return points == 200; } function display() { document.getElementById("demo").innerHTML = "index = "+points.findIndex(topRank); } </script> </body>
Output
Above, click the button “Result” −
- Related Questions & Answers
- Array findIndex() function in JavaScript
- JavaScript Array find() function
- JavaScript Array fill() function
- JavaScript Array some() function
- Array some() function in JavaScript
- Sorting Array with JavaScript reduce function - 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
- How to pass a PHP array to a JavaScript function?
- Date.getTime() function JavaScript
- Math.atan() function JavaScript
- Math.min() function JavaScript
- JavaScript Sleep() function?
Advertisements