Functions - JavaScript indexOf()
Posted by Superadmin on May 06 2023 02:40:16

JavaScript indexOf()

By Priya PedamkarPriya Pedamkar
  

JavaScript indexOf()

Introduction to JavaScript indexOf()

JavaScript indexOf() method used to find the occurrence of an item in an array. Method name indicates index means sequence values position. So, the indexOf() method 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 in a commerce website. It is very difficult 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 -1 value. The search will begin at the given position through the indexOf() method. Let suppose if 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 present more than once then indexOf() method returns first occurred item from the list.

Syntax:

Array_Name.indexOf (item,starting position);

Parameters:

Examples of JavaScript indexOf()

The 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:

Searching the index

Explanation:

As we can observe from the output we can clearly see if we pass existed items from an array is 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.

Note: Index position always starts from 0.

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:

javascript indexof

Explanation:

The above example we can see while we are accessing Cow from the array then prints 6 as the index position. While we are trying to access the Dog from the array then prints -1. It may really surprise the output. The animal array has Dog value but it prints -1 because we are starting search operation at 1st index position. So, the indexOf() method neglects the 0th index and starts searching from 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 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:

Mentioning a Position

Explanation:

If we pass a single character without position to indexOf() method then it starts searching from 0th So, searchStringIndex(“a”) returns 6th index as output. If we pass a single character with a position to indexOf() method then 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 indexOf() method, first it will check whether that sequence characters existed or not. If existed returns first index position in that sequence of characters. So, the searchStringIndex(“Paramesh”) gives 24 as output. Here P’s index position 24 so, it printed 24 as output. We can conclude when multiple characters 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:

javascript indexof

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 3 only but we are trying to access the 6th index value. So, it gives -1 as output.

Conclusion

indexOf() method has the only item, then it will start searching from the 0th index. indexOf() method has item and position, then it will start from specifying position index value. No search item results in -1 value.