How to use the indexOf() method In Javascript

The index of the first occurrence of a given substring can be obtained for strings using the built-in method indexOf() provided by JavaScript. The basic syntax of indexOf() is as follows.

Example: Here is an example to find the index of the first occurrence of a substring in a string using the indexOf() method.

Javascript
// Example string
const originalString = "This is a sample string";

// Target substring
const targetSubstring = "sample";

// Finding the index of the first occurrence of the target substring
const index = originalString.indexOf(targetSubstring);

console.log("Index of the first occurrence:", index);

Output
Index of the first occurrence: 10

JavaScript Program to find the Index of the First Occurrence of a Substring in a String

In this article, we will find the index of the first occurrence of a substring in a string using JavaScript.

An essential task in JavaScript programming is searching for substrings within a string. Finding the index of the first occurrence of a substring is a typical necessity, whether you are constructing a website, working with data processing, or simply altering the text.

Similar Reads

Methods to Find the Index of the First Occurrence of a Substring in a Given String:

JavaScript // Example string const originalString = "This is a sample string"; // Target substring const targetSubstring = "sample"; // Finding the index of the first occurrence of the target substring using a loop let index = -1; for (let i = 0; i <= originalString.length - targetSubstring.length; i++) { if (originalString.substring(i, i + targetSubstring.length) === targetSubstring) { index = i; break; } } console.log("Index of the first occurrence:", index);...

Method 1: Using the indexOf() method

The index of the first occurrence of a given substring can be obtained for strings using the built-in method indexOf() provided by JavaScript. The basic syntax of indexOf() is as follows....

Method 2: Using RegExp and match() method

The match() method of JavaScript can also be used to determine the index of the first instance of a substring when paired with regular expressions (RegExp)....

Method 3: Using ES6’s includes() method

The ES6 includes() method is mostly used to determine whether a string contains a particular substring, but with a little fiddling, it can also be used to get the index of the first occurrence....

Method 4: Using the search() method

Another approach for using regular expressions to determine the index of a substring’s first appearance is the search() method....

Method 5: Using ES6’s findIndex() method

The index of the first instance of a substring can be discovered using the array’s findIndex() method. But first, the string must be turned into a character array....

Method 6: Using a Loop with substring() and indexOf() Methods

This method involves iterating through the string and using the substring method to compare slices of the original string with the target substring....