Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
JavaScript

JavaScript String indexOf() Method

  • 30 Sep, 2025
  • Com 0
JavaScript String indexOf() Function

JavaScript String indexOf() is a built-in method that searches for the first occurrence of a specified substring within a string and returns its starting index. If it does not find a substring, it returns -1; otherwise, it returns the starting index. The indexOf() method is case-sensitive and zero-indexed.

Finding the index of the search element in JavaScript String

const input_string = "App, Dividend!";

console.log(input_string.indexOf("Dividend"));

// Output: 5

In this code, we are trying to find the starting index of the substring “Dividend”. Now, the first character of the main string is “A” and its index starts with 0.

Now, we want to find the index of “Dividend”, so its starting character is “D” and its index is 5. You also need to count “,” and “whitespace” as characters while scanning for the substring. Hence, the output is index 5.

Syntax

str.indexOf(search_string, from_index)

Parameters

Argument Description
search_string It represents the substring to search for. 
from_index (optional) It represents an index at which you can begin the search.

By default, its value is 0. If it is negative, it is treated as 0.

With the starting position

If you pass the from_index as a starting index for searching, it will start searching the substring with that specific index.

With the fromIndex argument in String.indexOf() method in JavaScript

const input_string = "App, Dividend!";

console.log(input_string.indexOf("dend", 6));

// Output: 9

In this code, we passed the from_index as index 6. Therefore, it will start searching from index 6 and proceed left-to-right. Since, it found the substring “dend” at starting index 9, it returns that index as an output.

Case sensitivity

The indexOf() is a strictly case-sensitive, and if the substring in an input string is “dend” and you are searching for “Dend”, it will return -1 because “Dend” does not exist in the main string.

No element found

const input_string = "App, Dividend!";

console.log(input_string.indexOf("Dend", 6));

// Output: -1

However, you can use string.toLowerCase() or regex for case-insensitive needs.

Empty search string

An empty string is considered found at any valid position, clamped between 0 and the length of the string.

const input_string = "App, Dividend!";

console.log(input_string.indexOf(""));

// Output: 0 (default behavior)

console.log(input_string.indexOf("", 2));

// Output: 2

console.log(input_string.indexOf("", 5));

// Output: 5

Position greater than length

If the starting position is greater than the length of the input string, it will return -1 as an output because it is not possible to find a substring outside of its length.

const input_string = "App, Dividend!";

console.log(input_string.indexOf("end", 20));

// Output: -1

Passing a negative position

If you pass a negative position, it searches from the start. If fromIndex (position) < 0, JavaScript treats it as 0.

const input_string = "App, Dividend!";

console.log(input_string.indexOf("dend", -10));

// Output: 9

Here, input_string.indexOf(“dend”, -10) is equivalent to input_string.indexOf(“dend”, 0). 

The substring “dend” starts at index 9 and is therefore included in the output.

That’s all!

Post Views: 7
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

Numpy.linalg.norm(): Find a Matrix or Vector Norm
JavaScript Array indexOf() Method

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of services

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend