Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is indexOf () in JavaScript?
2.1.
Syntax of indexOf()
3.
Using indexOf() with Strings
4.
Using indexOf() with Arrays
5.
Case Sensitivity and Return Values
6.
JavaScript String indexOf() method examples
6.1.
Basic Usage
6.2.
Javascript
6.2.1.
Output 
6.3.
Case-Sensitive Search
6.4.
Javascript
6.4.1.
Output
6.5.
Searching from a Specific Position
6.6.
Javascript
6.6.1.
Output
6.7.
Checking for Non-existence
6.8.
Javascript
6.8.1.
Output
6.9.
Finding All Occurrences
6.10.
Javascript
7.
Difference Between String indexOf() and String search()
8.
Frequently Asked Questions
8.1.
What is the use of indexOf in JavaScript?
8.2.
What is the index in JavaScript?
8.3.
What is the difference between indexOf and contains in JavaScript?
9.
Conclusion
Last Updated: Aug 31, 2024
Easy

JavaScript String indexOf() method

Author Lekhika
0 upvote

Introduction

The JavaScript String indexOf() method is a powerful tool that allows you to search for the first occurrence of a specified substring within a given string. It returns the index position of the substring if found, or -1 if the substring is not present, which makes it useful for various string manipulation tasks.

Indexof() in javascript

In this article will discuss the basics like its definition, syntax, examples, and use cases.

What is indexOf () in JavaScript?

The indexOf() method is a built-in JavaScript function that returns the first index at which a given element can be found in an array or string, or -1 if it is not present. In other words, it helps to find the position of a particular element in a string or array.

Syntax of indexOf()

Let's check out the basic syntax of the indexOf() method:

array.indexOf(element, start)

 

element: The element to locate in the array.

start (optional): The index to start the search from. If not specified, it starts from index 0.

Using indexOf() with Strings

Here's an example of using indexOf() with strings:

let str = "Hello, world!";
let position = str.indexOf("world");
console.log(position); 
You can also try this code with Online Javascript Compiler
Run Code

 

Using indexOf() with Strings

In this case, indexOf() returns 7, which is the starting position of the substring "world" in str.

Using indexOf() with Arrays

The indexOf() method works similarly with arrays. Here's an example:
 

let arr = ["Apple", "Banana", "Mango", "Orange"];
let index = arr.indexOf("Mango");
console.log(index); 
You can also try this code with Online Javascript Compiler
Run Code

 

Using indexOf() with Arrays

Here, indexOf() returns 2, which is the index of "Mango" in the array arr.

 

Case Sensitivity and Return Values

Remember that indexOf() is case-sensitive. It treats "a" and "A" as different characters. If indexOf() doesn't find the element, it returns -1.

JavaScript String indexOf() method examples

The indexOf() method in JavaScript is used to find the first occurrence of a substring within a given string. It returns the index of the first character of the first occurrence of the specified substring, or -1 if the substring is not found. Here are some examples of how to use the indexOf() method:

Basic Usage

  • Javascript

Javascript

const text = "Hello, world!";
const position = text.indexOf("world");
console.log(position);
You can also try this code with Online Javascript Compiler
Run Code

Output 

7

Case-Sensitive Search

  • Javascript

Javascript

const text = "Hello, World!";
const position = text.indexOf("world");
console.log(position);
You can also try this code with Online Javascript Compiler
Run Code

Output

-1 (not found, case-sensitive)

Searching from a Specific Position

  • Javascript

Javascript

const text = "This is a test. Is this test good?";
const position = text.indexOf("is", 5); // Start searching from index 5
console.log(position);
You can also try this code with Online Javascript Compiler
Run Code

Output

19

Checking for Non-existence

  • Javascript

Javascript

const text = "Hello, world!";
const position = text.indexOf("universe");
console.log(position);
You can also try this code with Online Javascript Compiler
Run Code

Output

-1 (not found)

Finding All Occurrences

  • Javascript

Javascript

const text = "This is a test. Is this test good?";
let position = text.indexOf("is");
while (position !== -1) {
console.log(`Found "is" at index ${position}`);
position = text.indexOf("is", position + 1);
}
You can also try this code with Online Javascript Compiler
Run Code

The indexOf() method is a useful tool for searching and manipulating strings in JavaScript. It's worth noting that there is also a related method called lastIndexOf(), which finds the last occurrence of a substring in a string.

Difference Between String indexOf() and String search()

 

AspectString indexOf()String search()
Return ValueReturns the index of the first occurrence of theReturns the index of the first occurrence of the
 specified substring, or -1 if not found.specified substring, or -1 if not found.
Syntaxstr.indexOf(searchValue, fromIndex)str.search(regexp)
Parameter TypeAccepts a string as the search value.Accepts a regular expression as the search pattern.
Case-sensitivityCase-sensitive by default.Case-sensitive by default, but can be modified using flags.
Start PositionOptional second parameter specifies the startAlways searches from the beginning of the string.
 position for the search. 
Regular ExpressionsDoes not support regular expressions as the searchSupports regular expressions as the search pattern,
 value.allowing for more advanced search capabilities.
Browser CompatibilitySupported in all major browsers.Supported in all major browsers.

Frequently Asked Questions

What is the use of indexOf in JavaScript?

In JavaScript, the indexOf method is used to determine the first index at which a given element can be found in an array or a substring in a string, returning -1 if not found.

What is the index in JavaScript?

In JavaScript, the index is the position of an element in an array or a character in a string, starting from 0.

What is the difference between indexOf and contains in JavaScript?

indexOf is a method that returns the index of the first occurrence of a value in a string or array, while includes (not "contains") returns a boolean indicating if a value is present.

Conclusion

In this article, we talked about the JavaScript String indexOf() method, which searches for a specified substring within a string and returns its index position. It is a simple but effective way to locate substrings and perform various string manipulation tasks in JavaScript.We also saw the difference between String indexOf() and String search().

To learn more about web development, we recommend reading the following articles:

If you liked our article, do upvote our article and help other ninjas grow.  You can refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingSystem Design, and many more!

Live masterclass