

findIndex ( arrayItem => arrayItem = "b" ) // 1ĮxampleArray.

Here is a visual example of the differences between method vs the method:ĮxampleArray. What this means is that indexOf will only ever look for a value in an array, whereas findIndex will let you decide how you find the index. The main difference between findIndex vs indexOf in JavaScript is that findIndex accepts a callback as an argument, and indexOf accepts a value as an argument. What is the difference between findIndex vs indexOf in JavaScript? Now, let’s get stuck in and see what the differences are between findIndex vs indexOf. In this post about findIndex vs indexOf in JavaScript we are going to look at the differences between the method and the method.īefore we get stuck into this article I have two posts, how to get the index of an item in an array in JavaScript, and how to get the index of an object in an array in JavaScript, that go into detail about how to use both of these methods and I would highly recommend having a read of them after this post. But in case of – numbers1 there is no prime number in the array thus -1 is returned.As you know there are two helpful methods for finding an index in a JavaScript array, but which one should you use? Here in case of – numbers = 17 is the prime number. Please click the buttonĪfter Click on the first “Let’s Check” Button.Īfter clicking on the second “Let’s Check” button. Let us find if any adult is there is queue or not. Examples to Implement findIndex() in JavaScriptįollowing are the example are given below: Here 2 means numbers = 19 is an odd number. Let us find the first Odd element in the array Please click the buttonĭocument.getElementById("demo").innerHTML = numbers.findIndex(isOdd) Index and array argument is also being passed. Here every element is checked for idd condition, and the index of the first odd element is returned. Here our function is idOdd and the value being passed using ages.findIndex() command. Let’s say a function is isOdd – that checks if the given value is odd or not. thisArg: This denotes the value that needs to be specified in “this value”.Array: This is an optional argument that denotes the array whose values are being checked for the given condition.

Index: This denotes the current index for which the function is called.currentValue: This denotes the value present at the current index in the array.
