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)
Call
What 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
Parameter
Description
searchString
Required. Substring to look for
position
Optional. Start index; negatives are treated as 0
Return Value
Return Value
Description
*
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!)
First letter only."w" first appears inside "new" at index 8.
Missing substring."xyz" is absent → -1.
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
Exact match."Blue" is at index 0; includes reports true.
Case matters."blue" does not match "Blue" → -1.
Case-insensitive search. Lowercase both sides (here, the string), then call indexOf.
Pick by need. Need the index → indexOf; only yes/no → includes.
Compatibility
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 ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeSupported · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
Samsung InternetSupported · Android
Full support
BunSupported · JavaScript runtime
Supported
DenoSupported · JavaScript runtime
Supported
Node.jsSupported · Server runtime
Supported
Android WebViewSupported · 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.