JavaScript String indexOf() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Method

Definition

String.prototype.indexOf(searchString) returns the first index where a substring starts. If it is missing, you get -1. Need only yes/no? Prefer includes(). Need the last match? Use lastIndexOf().

Pass the required arguments shown below. Check return values and browser support before you rely on the result.

Syntax

Call string.indexOf. Start with this line.

JavaScript
str.indexOf(searchString, position = 0)
CallWhat it does
str.indexOf(searchString, position = 0)Primary signature.
string.indexOf(...)Call with the arguments your case needs.

Built into JavaScript. Call indexOf(...) on a string (or String instance) — no import required.

Library declarations
JavaScript
str.indexOf(searchString, position = 0)

Parameters

ParameterDescription
searchStringRequired. Substring to look for
positionOptional. Start index; negatives are treated as 0

Return Value

Return ValueDescription
*Index (≥ 0) or -1

JavaScript Examples

Example 1 — Find the First Index

Get the position of a substring, or -1 when it is missing.

JavaScript
const text = "Brave new world";

console.log(text.indexOf("new"));  // 6
console.log(text.indexOf("w"));    // 8
console.log(text.indexOf("xyz"));  // -1
console.log(text.indexOf("B"));    // 0  (valid index!)
Try It Yourself

How It Works

  1. Match a word. "new" begins at index 6.
  2. First letter only. "w" first appears inside "new" at index 8.
  3. Missing substring. "xyz" is absent → -1.
  4. Index 0 is real. "B" is at the start → 0. Always check with !== -1.

Example 2 — Start Later with position

Skip an earlier match to find the next occurrence.

JavaScript
const text = "I think Ruth's dog is cuter than your dog!";
const needle = "dog";

const first = text.indexOf(needle);           // 15
const second = text.indexOf(needle, first + 1); // 38

console.log(first);
console.log(second);
console.log(text.indexOf(needle, 39));        // -1
Try It Yourself

How It Works

  1. First search. Finds the first "dog" at index 15.
  2. Search again. Pass first + 1 so the scan cannot re-match the same spot.
  3. Second hit. The later "dog" starts at 38.
  4. Past the end. Starting at 39 finds nothing → -1.

Example 3 — indexOf vs includes (and Case)

Use indexOf for the position; use includes for yes/no. Fold case when needed.

JavaScript
const text = "Blue Whale";

console.log(text.indexOf("Blue"));   // 0
console.log(text.includes("Blue"));  // true

console.log(text.indexOf("blue"));   // -1  (case differs)
console.log(text.toLowerCase().indexOf("blue")); // 0
Try It Yourself

How It Works

  1. Exact match. "Blue" is at index 0; includes reports true.
  2. Case matters. "blue" does not match "Blue" → -1.
  3. Case-insensitive search. Lowercase both sides (here, the string), then call indexOf.
  4. Pick by need. Need the index → indexOf; only yes/no → includes.

Browser & Runtime Support

String.prototype.indexOf() is a long-standing language feature. It works in every modern browser, Internet Explorer, and every current Node.js / Deno / Bun release.

✓ Baseline · Widely available

String.prototype.indexOf()

Supported in all evergreen browsers, Internet Explorer (legacy), and every current JavaScript runtime.

100% Browser support
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge Supported · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
String.indexOf() Universal

Bottom line: Safe everywhere. Prefer includes() for clearer membership checks in modern code, but indexOf() remains the right tool when you need the index.

Frequently Asked Questions

indexOf(searchString, position?) returns the zero-based index of the first occurrence of searchString at or after position. If nothing is found, it returns -1.
Yes. "Blue Whale".indexOf("blue") returns -1. Normalize with toLowerCase() when you need a case-insensitive search.
Compare with -1: str.indexOf(needle) !== -1. For a clearer boolean, prefer str.includes(needle).
With no position (or a position inside the string), it returns that position (default 0). If position is past the end, it returns str.length.
indexOf() returns the first index or -1. includes() returns true or false. Use indexOf when you need the position; use includes for simple yes/no checks.
No. Strings are immutable. indexOf() only reads and returns a number.

Next: lastIndexOf()

Find the last occurrence of a substring instead of the first.

lastIndexOf() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful