Syntax
str.indexOf(substring)
str.includes(substring)
str.startsWith(substring)
str.endsWith(substring)Examples
indexOf() and includes()
Finding whether and where a substring appears.
const text = "The quick brown fox";
console.log(text.indexOf("quick")); // 4
console.log(text.indexOf("lazy")); // -1 - not found
console.log(text.includes("brown")); // true
console.log(text.includes("Brown")); // false - case-sensitivestartsWith() and endsWith()
Checking the beginning or end of a string specifically.
const filename = "report.pdf";
console.log(filename.endsWith(".pdf")); // true
console.log(filename.endsWith(".docx")); // false
console.log(filename.startsWith("report")); // true
// Common use: validating file extensions or URL prefixes
const url = "https://example.com";
console.log(url.startsWith("https://")); // trueBest practices
- Use includes() for a simple presence check instead of indexOf() !== -1 - it is more readable
- Use startsWith()/endsWith() instead of manually slicing strings to check prefixes or suffixes
- Convert both sides with toLowerCase() first if you need a case-insensitive search
- Remember all these methods are case-sensitive by default - "Hello".includes("hello") is false
At a glance
- Purpose
- Application logic and interaction
- File extension
- .js ยท .mjs
- Runs in
- Browsers and JavaScript runtimes
- Usually used with
- HTML, CSS and Web APIs
Specifications & further reading
Related JavaScript documentation
String Basics: length, charAt(), at()
Strings are immutable sequences of characters - every string method returns a new string rather than modifying the original. The length property gives the character count. charAt() returns the character at a given index, while the newer at() method also supports negative indices to count from the end.slice(), substring(), substr()
These three methods extract a portion of a string as a new string. slice() and substring() are similar, but slice() accepts negative indices (counting from the end) while substring() treats negative values as 0 and swaps arguments if start is greater than end. substr() (deprecated) uses a start index and a length rather than an end index.replace() and replaceAll()
replace() returns a new string with the first match of a pattern replaced by a new value - or all matches, if the pattern is a regex with the global (g) flag. replaceAll(), introduced in ES2021, always replaces every occurrence without needing a regex, making simple find-and-replace-all operations more straightforward.split()
split() divides a string into an array of substrings based on a specified separator, which can be a literal string or a regular expression. It is the inverse operation of Array.prototype.join(). An optional second argument limits the number of resulting elements.
Strings are immutable sequences of characters - every string method returns a new string rather than modifying the original. The length property gives the character count. charAt() returns the character at a given index, while the newer at() method also supports negative indices to count from the end.slice(), substring(), substr()
These three methods extract a portion of a string as a new string. slice() and substring() are similar, but slice() accepts negative indices (counting from the end) while substring() treats negative values as 0 and swaps arguments if start is greater than end. substr() (deprecated) uses a start index and a length rather than an end index.replace() and replaceAll()
replace() returns a new string with the first match of a pattern replaced by a new value - or all matches, if the pattern is a regex with the global (g) flag. replaceAll(), introduced in ES2021, always replaces every occurrence without needing a regex, making simple find-and-replace-all operations more straightforward.split()
split() divides a string into an array of substrings based on a specified separator, which can be a literal string or a regular expression. It is the inverse operation of Array.prototype.join(). An optional second argument limits the number of resulting elements.