JavaScript Array indexOf()
Syntax & Examples
Array.indexOf() method
The indexOf() method of the Array class in JavaScript returns the first (least) index at which a given element can be found in the calling array. If the element is not found, it returns -1.
Syntax of Array.indexOf()
There are 2 variations for the syntax of Array.indexOf() method. They are:
indexOf(searchElement)
Parameters
Parameter | Optional/Required | Description |
---|---|---|
searchElement | required | The element to locate in the array. |
This method returns the first (least) index at which a given element can be found in the calling array, starting the search at the beginning of the array.
Returns value of type Number
.
indexOf(searchElement, fromIndex)
Parameters
Parameter | Optional/Required | Description |
---|---|---|
searchElement | required | The element to locate in the array. |
fromIndex | optional | The index to start the search at. Defaults to 0. |
This method returns the first (least) index at which a given element can be found in the calling array, starting the search at the specified fromIndex.
Returns value of type Number
.
✐ Examples
1 Using indexOf() method to find the index of an element
In JavaScript, we can use the indexOf() method to find the index of a specified element in an array.
For example,
- We define an array variable arr with elements [1, 2, 3, 4, 5].
- We use the indexOf() method with the value 3 to find the index of the element 3.
- The result is stored in the variable indexOfThree.
- We log indexOfThree to the console using console.log() method to see the index of the element 3.
JavaScript Program
const arr = [1, 2, 3, 4, 5];
const indexOfThree = arr.indexOf(3);
console.log(indexOfThree);
Output
2
2 Using indexOf() method to find the index of an element starting from a specific index
We can use the indexOf() method to find the index of a specified element in an array, starting the search from a specific index.
For example,
- We define an array variable arr with elements [1, 2, 3, 4, 5, 3].
- We use the indexOf() method with the value 3 and the fromIndex 3 to find the index of the element 3 starting from index 3.
- The result is stored in the variable indexOfThreeFromIndex3.
- We log indexOfThreeFromIndex3 to the console using console.log() method to see the index of the element 3 starting from index 3.
JavaScript Program
const arr = [1, 2, 3, 4, 5, 3];
const indexOfThreeFromIndex3 = arr.indexOf(3, 3);
console.log(indexOfThreeFromIndex3);
Output
5
3 Using indexOf() method to find the index of a string in an array
We can use the indexOf() method to find the index of a specified string in an array.
For example,
- We define an array variable strArr with elements ['apple', 'banana', 'cherry'].
- We use the indexOf() method with the value 'banana' to find the index of the string 'banana'.
- The result is stored in the variable indexOfBanana.
- We log indexOfBanana to the console using console.log() method to see the index of the string 'banana'.
JavaScript Program
const strArr = ['apple', 'banana', 'cherry'];
const indexOfBanana = strArr.indexOf('banana');
console.log(indexOfBanana);
Output
1
Summary
In this JavaScript tutorial, we learned about indexOf() method of Array: the syntax and few working examples with output and detailed explanation for each example.