Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Understanding indexOf()
2.1.
Syntax of indexOf()
2.2.
Using indexOf() with Strings
2.3.
Using indexOf() with Arrays
3.
Case Sensitivity and Return Values
4.
JavaScript String indexOf() method examples
4.1.
Basic Usage
4.2.
Javascript
4.2.1.
Output 
4.3.
Case-Sensitive Search
4.4.
Javascript
4.4.1.
Output
4.5.
Searching from a Specific Position
4.6.
Javascript
4.6.1.
Output
4.7.
Checking for Non-existence
4.8.
Javascript
4.8.1.
Output
4.9.
Finding All Occurrences
4.10.
Javascript
5.
Frequently Asked Questions
5.1.
What is indexOf () in JavaScript?
5.2.
What is the index in JavaScript?
5.3.
What is the difference between indexOf and contains in JavaScript?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

JavaScript String indexOf() method

Author Lekhika
0 upvote
gp-icon
Basics of Javascript
Free guided path
10 chapters
68+ problems
gp-badge
Earn badges and level up

Introduction

Today, we will discuss the indexOf() method in JavaScript, a crucial function for handling strings and arrays.

Indexof() in javascript

This article will guide you through the basic definition, syntax, examples, and use-cases, giving you the confidence to apply this method in your daily coding journey.

Understanding indexOf()

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

 

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

 

Using indexOf() with Arrays

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

 

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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

Output 

7

Case-Sensitive Search

  • Javascript

Javascript

const text = "Hello, World!";
const position = text.indexOf("world");
console.log(position);

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

Output

19

Checking for Non-existence

  • Javascript

Javascript

const text = "Hello, world!";
const position = text.indexOf("universe");
console.log(position);

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

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.

Frequently Asked Questions

What is indexOf () in JavaScript?

The indexOf() in JavaScript is a method used to find the first occurrence of a substring within a string and returns its starting index or -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

The indexOf() method in JavaScript is a powerful function that simplifies the task of searching for a specific value in a string or array. As you continue to harness its potential in various use cases, you'll find it an indispensable part of your JavaScript toolkit. 

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!

Previous article
How to Convert String to Number in Javascript?
Next article
Number.prototype.toLocaleString() in JavaScript
Guided path
Free
gridgp-icon
Basics of Javascript
10 chapters
68+ Problems
gp-badge
Earn badges and level up
Live masterclass