Articles by FavTutor
  • AI News
  • Data Structures
  • Web Developement
  • AI Code GeneratorNEW
  • Student Help
  • Main Website
No Result
View All Result
FavTutor
  • AI News
  • Data Structures
  • Web Developement
  • AI Code GeneratorNEW
  • Student Help
  • Main Website
No Result
View All Result
Articles by FavTutor
No Result
View All Result
Home Web Developement

JavaScript Array findIndex() Method Explained

Komal Bhatia by Komal Bhatia
December 26, 2023
Reading Time: 5 mins read
JavaScript Array findIndex() Method
Follow us on Google News   Subscribe to our newsletter

Do you know about findIndex() method in JS? As we know, arrays are linear data structures that store homogenous items i.e. can not be a combination of integers and characters, or integers or strings. These items are indexed in them. JavaScript also provides the ability to locate the index of any data item we want. In this article, we will look carefully into the findIndex() method in JavaScript, a powerful tool for this use case.

What is the findIndex() Method in JavaScript?

In any programming, finding the index of an element in an array is a common task. The main reason is to locate the position of a specific value within the array so that the developer can then manipulate the element according to that index. This can be crucial when working with large datasets. Also, in situations when we need to validate the existence or absence of an element, it becomes relevant.

The findIndex() is a built-in JavaScript method that is used to search through an array and find the index of the first element that satisfies a given condition. If the element is found, the method returns the index of the element. Otherwise, the method returns -1 which justifies that there’s no element in the array matching the condition of the method.

It is important to remember that the method is not mutated, which means that it doesn’t modify the original array. 

The syntax of the method is given below:

array_name.findIndex(function(currValue, index, arr), thisValue);

where,

  • array_name: The name of the array on which the search has to be performed.  
  • function: Function will run for every element of the array
  • currValue: This holds the current value
  • index: Optional Parameter holding the index of the current value.
  • arr: Optional parameter to which the current element belongs
  • thisValue: A value passed to the function as this value.

Here’s an example of the same:

//declaring a function 
function checkMarks(marks){
    return marks>90;
}

//declaring an array
let marks=[54,67,97,92,83,74,95];

//using the findIndex 
let index=marks.findIndex(checkMarks);

//printing the index of the first element that passed the condition
console.log(index);

Output:

2

In this example, we have made a function to checkMarks which checks whether the marks are greater than 90. We use this function as a parameter in the findIndex method which returns the index as 2 indicating that element 97 is greater than 90. Hence, we have obtained the desired result. 

Let us see another example, where the method will return -1:

//declaring a function 
function checkMarks(marks){
    return marks>99;
}

//declaring an array
let marks=[54,67,97,92,83,74,95];

//using the findIndex 
let index=marks.findIndex(checkMarks);

//printing the index of first element that passed the condition
console.log(index);

Output:

-1

Here, we are using the checkMarks function with the condition of returning marks greater than 99. Hence, when this function is used as a parameter in the method, it will not return true for any element in the array. Hence, no element satisfies the condition due to which no index can be returned. The method returns -1.

Note that it stops iterating through the array as soon as it finds the first element that satisfies the condition. It does not continue to search for additional matches.

JavaScript findIndex() on Empty Arrays

Let us see how this method performs on the empty arrays:

//declaring a function 
function checkMarks(marks){
    return marks>90;
}

//declaring an array
let marks=[];

//using the findIndex 
let index=marks.findIndex(checkMarks);

//printing the index of the first element that passed the condition
console.log(index);

Output:

-1

There are no elements in the array on which we have to perform the function, so no index will be found. Hence, the method returns -1.

What is the alternative to findIndex?

The best alternative to the findIndex() method in JavaScript is the find() method. Both of them have the same purpose but the biggest difference is in what they return. The find method returns the first element in an array that satisfies the provided testing function, while findIndex returns the index of that element.

Now you can move on to learn about how to append elements to JavaScript array.

Conclusion

In this article, we have explored the JavaScript findIndex() method, and how to use it with simple arrays and empty arrays. You can start using this method in your JavaScript code and enhance your array manipulation capabilities today!

ShareTweetShareSendSend
Komal Bhatia

Komal Bhatia

I am a dedicated Computer Science student with a strong track record of academic excellence. I am a highly motivated individual with a passion for providing creative and innovative solutions to complex problems. I possess skills in the domain of C++ and web development and am always eager to contribute to the field of Software Development.

RelatedPosts

Javascript Animation Libraries

Top 10 JavaScript Animation Libraries in 2025

February 19, 2025
JavaScript Interview Questions

Top 40 JavaScript Interview Questions and Answers in 2024

April 1, 2025
Best React UI Component Libraries

10 Must-Know React UI Component Libraries for Web Devs 2024

May 7, 2024
Currying in JavaScript

Currying in JavaScript Explained (with Examples)

March 15, 2024
Javascript Format Currency

Javascript Program for Format Currency

April 8, 2025

About FavTutor

FavTutor is a trusted online tutoring service to connects students with expert tutors to provide guidance on Computer Science subjects like Java, Python, C, C++, SQL, Data Science, Statistics, etc.

Categories

  • AI News, Research & Latest Updates
  • Trending
  • Data Structures
  • Web Developement
  • Data Science

Important Subjects

  • Python Assignment Help
  • C++ Help
  • R Programming Help
  • Java Homework Help
  • Programming Help

Resources

  • About Us
  • Contact Us
  • Editorial Policy
  • Privacy Policy
  • Terms and Conditions

Website listed on Ecomswap. © Copyright 2025 All Rights Reserved.

No Result
View All Result
  • AI News
  • Data Structures
  • Web Developement
  • AI Code Generator
  • Student Help
  • Main Website

Website listed on Ecomswap. © Copyright 2025 All Rights Reserved.