JavaScript and JQuery Essentials Trainin... (56 Blogs) Become a Certified Professional
AWS Global Infrastructure

Front End Web Development

Topics Covered
  • AngularJS (29 Blogs)
  • jQuery UI Development (2 Blogs)
  • The Complete WebDeveloper (38 Blogs)
  • ReactJS (7 Blogs)
SEE MORE

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-widget-banner-bg

What is indexOf in JavaScript and how does it work?

Published on Oct 07,2019 218 Views
6 / 29 Blog from JavaScript

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-mobile-banner-bg

myMock Interview Service for Real Tech Jobs

  • Mock interview in latest tech domains i.e JAVA, AI, DEVOPS,etc
  • Get interviewed by leading tech experts
  • Real time assessment report and video recording

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.

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’)

Output:

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.

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");

Output:

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");

Output:

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 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.

Got a question for us? Please mention it in the comments section of “indexOf in JavaScript” blog and we will get back to you. 

Comments
0 Comments

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
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.