There are different built-in functions in JavaScript for different tasks. The Indexof function is used to return the starting position of the word. It basically returns the index of the particular element in the array. It is a function of Array that returns the index of an element within that Array.
What is indexOf in JavaScript?
IndexOf function is used to find out the index of an element in a String and an Array. There are two variations, one is indexOf that is used to give you the position of search element or the first matched element:
If element is not found it will return -1.
The second one is lastIndexOf function which is used to find the element from the last in a string or in an array.
Example:
var string =’Welcome to Edureka’ string.indexOf(‘Welcome’)
Output:
0
For the lastIndexof function, we can take the following example:
var = ‘Welcome to edureka’ string.lastIndexOf(‘edureka’)
12
The indexOf in JavaScript works in a similar way for arrays. Now let’s get into the depth of String and Array indexOf method with examples.
Enhance your design skills with our top-rated UI UX Design Course.
Javascript String indexOf
The str.indexOf() function finds the index of the first occurrence of the argument string in the given string. The value returned is 0-based. Also, it returns -1 if the value to search for never occurs.
Syntax:
string.indexOf(searchvalue, start)
The first argument, searchValue is the string that is to be searched in the base string. The second argument to the function index defines the starting index from where the searchValue is to be searched in the base string.
This function returns the index of the string (0-based) where the searchValue is found for the first time. If the searchValue cannot be found in the string then the function returns -1.
Example:
var str = "Hello, welcome to edureka!"; var n = str.indexOf("e");
1
Array indexOf in JavaScript
The arr.indexOf() function is used to find the index of the first occurrence of the search element provided as the argument to the function.
Syntax:
arr.indexOf(searchElement[,index])
The first argument, searchElement is the value to be searched in the array. The second argument to this function is the optional index argument which defines the starting index in the array from where the element is to be searched. If this argument is not provided then index 0 is taken as the starting index to begin the search as it is the default value.
This function returns the index of the first occurrence of the searchElement. If the element cannot be found in the array, then this function returns -1.
Example:
var cars = ["BMW", "Audi", "Ferrari"]; var arr = cars.indexOf("Ferrari");
2
Difference between indexOf() and search()
Both the indexOf() and search() methods are used to check whether a substring exists in the string or not. Also, it returns either of the indexes of the substring or -1 if the substring does not exist in the string. But there is a difference between indexOf() and search() methods. Let’s have a look at the syntax of the two methods.
Syntax of indexOf() method:
String.indexOf(substring, [offset]);
Syntax of search() method:
String.search(substring);
Now, you can see that in indexOf() method, there is an optional parameter(offset) from where we can start the searching but the method search() does not have this feature. It simply takes the substring and starts searching from the 0th index.
This was all about the indexOf in JavaScript. With this, we have come to the end of our article. I hope you understood how this method works in JavaScript.
Check out our Full Stack Developer Course which comes with instructor-led live training and real-life project experience. This training makes you proficient in skills to work with back-end and front-end web technologies. It includes training on Web Development, jQuery, Angular, NodeJS, ExpressJS, and MongoDB.
Check out the Angular Certification Training Course by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. Angular is a JavaScript framework that is used to create scalable, enterprise, and performance client-side web applications. With Angular framework adoption being high, performance management of the application is community-driven indirectly driving better job opportunities.
Got a question for us? Please mention it in the comments section of “indexOf in JavaScript” blog and we will get back to you.