AngularJS (55 Blogs) Become a Certified Professional
AWS Global Infrastructure

Front End Web Development

Topics Covered
  • AngularJS (45 Blogs)
  • The Complete WebDeveloper (34 Blogs)
  • ReactJS (14 Blogs)
  • JavaScript and JQuery Essentials Training (2 Blogs)

What is indexOf in JavaScript and how does it work?

Last updated on Apr 25,2023 5.3K Views

6 / 31 Blog from JavaScript

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. In this article, we will understand the indexOf in JavaScript in the following sequence:

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.


var string =’Welcome to Edureka’




For the lastIndexof function, we can take the following example:

var = ‘Welcome to edureka’




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.


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.


var str = "Hello, welcome to edureka!";
var n = str.indexOf("e");



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.



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.


var cars = ["BMW", "Audi", "Ferrari"];
var arr = cars.indexOf("Ferrari");



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

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 Web Developer Masters Program 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 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. 

Upcoming Batches For Angular Certification Course Online
Course NameDateDetails
Angular Certification Course Online

Class Starts on 27th July,2024

27th July

SAT&SUN (Weekend Batch)
View Details
Angular Certification Course Online

Class Starts on 31st August,2024

31st August

SAT&SUN (Weekend Batch)
View Details

Join the discussion

Browse Categories

webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

What is indexOf in JavaScript and how does it work?