Skip to content

Find index in Javascript: How to use JS inbuilt methods

Find index in Javascript- How to use JS inbuilt methods

There are multiple way using which we can find index in Javascript. We will use indexOf, findIndex and lastIndexOf methods. Find below explanation and examples for the same.

Use indexOf

In JavaScript, the indexOf() method is used to find the index of a specified value in an array. Here’s an example:

const fruits = ['apple', 'banana', 'orange'];

const index = fruits.indexOf('banana');
console.log(index); // output: 1

In the above example, the indexOf() method is called on the fruits array to find the index of the value 'banana'. The method returns the index 1 since 'banana' is located at that position in the array.

If the specified value is not found in the array, the indexOf() method returns -1. Here’s an example:

const fruits = ['apple', 'banana', 'orange'];

const index = fruits.indexOf('grape');
console.log(index); // output: -1

In the above example, the indexOf() method is called on the fruits array to find the index of the value 'grape'. Since 'grape' is not in the array, the method returns -1.

Use findIndex

Another method to find the index of a value in an array in JavaScript is the findIndex() method. This method returns the index of the first element in the array that satisfies the provided testing function. Here’s an example:

const numbers = [1, 5, 10, 15];

const index = numbers.findIndex(element => element > 8);
console.log(index); // output: 2

In the above example, the findIndex() method is called on the numbers array to find the index of the first element that is greater than 8. The method returns the index 2 since 10 is the first element in the array that satisfies the condition.

If none of the elements in the array satisfy the testing function, the findIndex() method returns -1. Here’s an example:

const numbers = [1, 5, 10, 15];

const index = numbers.findIndex(element => element > 20);
console.log(index); // output: -1

In the above example, the findIndex() method is called on the numbers array to find the index of the first element that is greater than 20. Since there is no element in the array that satisfies the condition, the method returns -1.

Use lastIndexOf

Another method to find the index of a value in an array in JavaScript is the lastIndexOf() method. This method returns the index of the last occurrence of a specified value in an array. Here’s an example:

const numbers = [1, 5, 10, 5, 15];

const index = numbers.lastIndexOf(5);
console.log(index); // output: 3

In the above example, the lastIndexOf() method is called on the numbers array to find the index of the last occurrence of the value 5. The method returns the index 3 since the last occurrence of 5 is at position 3 in the array.

If the specified value is not found in the array, the lastIndexOf() method returns -1. Here’s an example:

const numbers = [1, 5, 10, 5, 15];

const index = numbers.lastIndexOf(20);
console.log(index); // output: -1

In the above example, the lastIndexOf() method is called on the numbers array to find the index of the last occurrence of the value 20. Since 20 is not in the array, the method returns -1.

Hope you might have gotten some understanding about how we can use the inbuilt Javascript methods to find the index. 🙂

Please share