Search Here

Working with Javascript Array findIndex() method with examples for Beginners

Working with Javascript Array findIndex() method with examples for Beginners

Javascript array findIndex() method is a built-in prototype method in the array. This method returns the index of the matched items respective to which the first condition is matched. It returns only one index encountered even if the condition matches other items.

This method takes two arguments the first is a function and the second one is an object which is passed to the function and this can be accessed inside the function using this keyword.

Syntax

array.findIndex(function(current_value, index, array){}, extra_arguments);

This method does not modify or changes the existing array, returns a matched array index if an item is matched to the condition, and if no item is matched to the condition the return -1.

While passing arguments the first argument must be a function it can be named or an anonymous function and the second argument must object which is optional.
If you don’t pass a function as an argument than it throws Uncaught TypeError: is not a function an exception.
All the arguments in the function are optional except the number parameter in the function.

First argument in findIndex() method takes three arguments they are current index value, index of the array, and complete array all these arguments are optional but it is excepted that you must pass current_value argument.

If you wish to pass additional arguments to function than you can make use of the second argument extra_arguments as objects.
To access extra_arguments in function use, this object returns additional parameters passed to the function.

Table of Contents

Program to get an index of non-negative even number using array findIndex() method

Variable arr positive and negative numbers. Some of them are even and some are negative even number. We need to find an index of the first number which is non-negative and also an even number.
This logic is written inside find_non_negative_even_no a function which returns index number if the number matches given condition or else returns -1.

let arr = [1,-6,3,-8,4,12];
    
function find_non_negative_even_no(number, index, arr){
    if(number>0 && number%2==0){
        return true;  
    }
}

let result = arr.findIndex(find_non_negative_even_no);
console.log("The index of first Non-Negative Even Number is : "+result);

//Console Output
The index of first Non-Negative Even Number is : 4
Javascript find a non negative number using findIndex() method

Javascript find a non negative number using findIndex() method

If their is not even number exists the result will be -1.

let arr = [1, -7, 3, -15, -23, -11];
    
function my_func(number, index, arr){
    if(number%2==0){
        return true;  
    }
}

let result = arr.findIndex(my_func);
if(result!=-1){
    console.log("The Even Number exists in index : "+result);
}else{
    console.log("No even number exists in array. ");
}

//Console Output
No even number exists in array. 
Javascript check if even numbers exists in array using findIndex() method.

Javascript check if even numbers exists in array using findIndex() method.

Conclusion

We have reached the conclusion of our post Working with Javascript Array findIndex() method with examples for Beginners. Comment if you have any doubts and if you like then help us grow by sharing this post.

Related Posts

Summary
Review Date
Reviewed Item
Working with Javascript Array findIndex() method with examples for Beginners
Author Rating
51star1star1star1star1star
Software Name
Javascript Programming
Software Name
Windows Os, Mac Os, Ubuntu Os
Software Category
Web Development