
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
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 Articles
- 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
- JavaScript function that should count all unique items in an array
- JavaScript function to prepend string into all the values of array?
- Function to flatten array of multiple nested arrays without recursion in JavaScript
- Remove duplicate items from an array with a custom function in JavaScript
- JavaScript function that lives on the prototype object of the Array class

Advertisements