Updated June 30, 2023
Introduction to JavaScript indexOf()
The JavaScript indexOf() method finds an item’s occurrence in an array. Method name indicates index means sequence values position. So, the indexOf() method is always used with an array. indexOf() position always starts with 0 and ends with N-1. N is an array length.
Real-time example: I have one lakh items on a commerce website. It is tough to search manually, one by one. So, if we know the item name, then we can search. In this case, we can use the indexOf() method and get the index position.
How does the indexOf() method work in JavaScript?
JavaScript indexOf() method always works for the search operation. If the item is not found, it returns a -1 value. The search will begin at the given position through the indexOf() method. Suppose we did not mention any start position; then, the search starts from the 0th index. Like indexOf(“any value or string”,1(position)). While searching the specified item in the indexOf() method, if an item is present more than once, then the indexOf() method returns the first occurred item from the list.
Syntax:
Array_Name.indexOf (item,starting position);
Parameters:
- Item: Pass the required item from an array.
- Starting position: Pass the number to where to start the search operation.
Examples of JavaScript indexOf()
Examples of the following are given below:
Example #1
Searching the index of a book from an array (without mentioning a position): Below example, we do not specify any position within the indexOf() method, so the search starts at the 0th.
Code:
<!DOCTYPE html>
<html>
<head>
<title>IndexOf</title>
</head>
<body>
<font color="green">
<h1>Searching an Index Position of an Item</h1>
</font>
<script>
function searchIndex(name)
{
var book=["Maths","Physics","Biology","Chemistry","English","Polity","Economics"];
return book.indexOf(name);
}
var book1="English";
var book2="Zoology";
var indexOfBook=searchIndex(book1);
var indexOfBookNotExisted=searchIndex(book2);
document.write(book1+" position is :"+indexOfBook+"<br>");
document.write(book2+" position is :"+indexOfBookNotExisted);
</script>
</body>
</html>
Output:
Explanation:
As we can observe from the output, we can see if we pass existed items from an array given to the indexOf() method. It will return the actual index position. Whereas if we pass not existed value to the indexOf() method, it will return -1.
Example #2
Searching the index of an animal from an array (mentioning a position): Below example, we specify any position within the indexOf() method, so the search starts at the specified index.
Code:
<!DOCTYPE html>
<html>
<head>
<title>IndexOf</title>
</head>
<body>
<font color="green">
<h1>Searching an Index Position of an Item</h1>
</font>
<script>
function searchIndexOfAnimal(animal,postion)
{
var animal=["Dog","Cat","Monkey","Rabbit","Tiger","Lion","Cow"];
return animal.indexOf(animal,postion);
}
var animal1="Cow";
var animal2="Dog";
var indexOfAnimal=searchIndexOfAnimal(animal1,1);
var indexOfAnimalNotExisted=searchIndexOfAnimal(animal2,1);
document.write(animal1+" position is : "+indexOfAnimal+"<br>");
document.write(animal2+" position is : "+indexOfAnimalNotExisted);
</script>
</body>
</html>
Output:
Explanation:
In the above example, we can see while we are accessing Cow from the array, then prints 6 as the index position. While trying to access the Dog from the array, then prints -1. It may surprise the output. The animal array has a Dog value but prints -1 because we are starting the search operation at the 1st index position. So, the indexOf() method neglects the 0th index and starts searching from the 1st.
Example #3
Searching the index of a character from an array (mentioning a position): In a String, every character considers as the particular index. Example: “How” is a string with the H=0th, o=1st, and w=2nd index positions, respectively.
Code:
<!DOCTYPE html>
<html>
<head>
<title>IndexOf</title>
</head>
<body>
<font color="green">
<h1>Searching an Index Position of an Item from String</h1>
</font>
<script>
function searchStringIndex(character,postion)
{
var string="Hi, I am Amardeep. I am Paramesh.";
return string.indexOf(character,postion);
}
var c1="a";
var c2="am";
var c3="Paramesh";
var indexOfStr1=searchStringIndex(c1);
var indexOfStr2=searchStringIndex(c2,9);
var indexOfStr3=searchStringIndex(c3);
document.write(c1+" position is : "+indexOfStr1+"<br>");
document.write(c2+" position is : "+indexOfStr2+"<br>");
document.write(c3+" position is : "+indexOfStr3);
</script>
</body>
</html>
Output:
Explanation:
If we pass a single character without position to the indexOf() method, it starts searching from 0th. So, searchStringIndex(“a”) returns the 6th index as output. If we pass a single character with a position to the indexOf() method, it starts searching from the specified index. So, the searchStringIndex(“am,” 9) returns the 21st index as output. It will neglect before 8 indexes of string.
If we pass multiple characters at a time to the indexOf() method, first, it will check whether that sequence of characters existed or not. If it existed, it returns the first index position in that sequence of characters. So, the searchStringIndex(“Paramesh”) gives 24 as output. Here P’s index position is 24, so it printed 24 as output. We can conclude when multiple characters are in sequence, if those are present in a string, it will print the first character index.
Example #4
Access exceeding index from an array:
Code:
<!DOCTYPE html>
<html>
<head>
<title>IndexOf</title>
</head>
<body>
<font color="green">
<h1>Accessing Exceeded index from an array</h1>
</font>
<script>
function getIndex(name,postion)
{
var array=["1","Apple","@12","Paramesh"];
return array.indexOf(name,postion);
}
var name="Paramesh";
var indexOfArray=getIndex(name,6);
document.write(name+" position is : "+indexOfArray);
</script>
</body>
</html>
Output:
Explanation:
Above, we are trying to access out of the bounded index of 6. First, the indexOf() method checks whether the given position is within the actual position limit or not. An actual position limit is only 3, but we are trying to access the 6th index value. So, it gives -1 as output.
Conclusion
indexOf() method has the only item; it will start searching from the 0th index. indexOf() method has item and position, then it will begin by specifying the position index value. No search item results in a -1 value.
Recommended Articles
This is a guide to JavaScript indexOf(). Here we discuss the introduction, how the indexOf() method works in JavaScript, and the examples. You can also go through our other related articles to learn more–