JavaScript String Includes startsWith and endsWith Guide
Learn how to check if a JavaScript string contains, starts with, or ends with a substring using includes, startsWith, and endsWith with practical examples.
JavaScript provides three methods for checking whether a string contains a substring: includes, startsWith, and endsWith. All three return a straightforward true or false with no loops or regular expressions required.
const text = "JavaScript is everywhere";
console.log(text.includes("Script"));
console.log(text.startsWith("Java"));
console.log(text.endsWith("where"));All three print true. Replace "Script" with "script" (lowercase s) and every result flips to false. These methods are case-sensitive, which is the most common surprise for beginners.
For an overview of where these fit among all string operations, see the JavaScript string methods guide. For string extraction methods that return portions of text, see the guide to slice, substring, and substr.
includes()
The includes method checks if a substring appears anywhere in the string. It accepts an optional second argument: the position to start searching from, counting from zero.
const quote = "The quick brown fox jumps over the lazy dog";
console.log(quote.includes("fox"));
console.log(quote.includes("cat"));
console.log(quote.includes("quick", 5));The first call finds "fox" and returns true. The second finds no "cat" and returns false.
The third starts searching from position 5. Since "quick" begins at position 4, the search starting at 5 misses it entirely.
The position argument is useful when you need to find occurrences after a certain point in the string:
const word = "banana";
console.log(word.includes("ana"));
console.log(word.includes("ana", 2));
console.log(word.includes("ana", 4));The substring "ana" appears at positions 1 and 3. Searching from position 2 catches the second occurrence and returns true. Searching from position 4 skips both occurrences and returns false.
startsWith()
The startsWith method checks if the string begins with the given substring. Its optional position argument means "treat this character index as the new start of the string."
const url = "https://rune.codes/tutorials";
console.log(url.startsWith("https"));
console.log(url.startsWith("http"));
console.log(url.startsWith("rune", 8));The first call returns true because the URL starts with the secure protocol. The second returns false because the URL does not begin with plain "http". The third treats position 8 as the new starting point, where the characters begin with "rune", so it returns true.
This position behavior lets you check prefixes beyond the absolute beginning:
const path = "/users/profile/settings";
console.log(path.startsWith("/users"));
console.log(path.startsWith("profile", 7));Both print true. The second check asks: starting from position 7, does the string begin with "profile"? At position 7 the remaining text is "profile/settings", so the answer is yes.
endsWith()
The endsWith method checks if the string ends with the given substring. Its optional second argument is not a start position but a length limit. It treats the string as if only the first N characters exist, then checks the ending.
const filename = "report.pdf";
console.log(filename.endsWith(".pdf"));
console.log(filename.endsWith(".doc"));
console.log(filename.endsWith("port", 6));The first check returns true for the PDF extension. The second returns false.
The third considers only the first 6 characters of "report.pdf", which are "report". That truncated string does end with "port", so it returns true.
The difference between the second argument behavior trips up many beginners:
| Method | Second argument means |
|---|---|
| includes(sub, pos) | Start searching at position pos |
| startsWith(sub, pos) | Treat position pos as the beginning |
| endsWith(sub, len) | Only consider the first len characters |
Remember: startsWith uses a start position. endsWith uses an end length. They are not the same concept.
Case sensitivity
All three methods are case-sensitive. This fails silently rather than throwing an error:
const lang = "JavaScript";
console.log(lang.includes("javascript"));
console.log(lang.startsWith("java"));Both print false because the casing does not match.
To do case-insensitive checks, normalize both the source string and the search term to the same case before comparing. The most common approach is to call toLowerCase on both sides:
const lang = "JavaScript";
const search = "javascript";
console.log(lang.toLowerCase().includes(search.toLowerCase()));This now prints true because both strings are compared in lowercase. Normalizing casing first is a common pattern for search bars, filters, and any user-facing text matching where casing should not matter.
Practical use cases
Checking URL protocols is a direct application of startsWith. Filtering by file extension uses endsWith. Simple search filtering uses includes.
Checking protocols with startsWith:
function isSecure(url) {
return url.startsWith("https://");
}
console.log(isSecure("https://rune.codes"));This returns true for secure URLs and false for plain HTTP ones. The startsWith method checks the very beginning of the string, making it perfect for protocol validation.
Filtering files by extension uses endsWith in a similar way.
const files = ["photo.jpg", "notes.pdf", "script.js", "image.png"];
const images = files.filter(f => f.endsWith(".jpg") || f.endsWith(".png"));
console.log(images);This produces ["photo.jpg", "image.png"]. The endsWith method checks only the ending of each filename, so files with different extensions are automatically excluded.
Searching through a list of names works the same way with includes.
const names = ["Alex Chen", "Jordan Lee", "Taylor Smith", "Alex Rivera"];
const matches = names.filter(name => name.includes("Alex"));
console.log(matches);This produces ["Alex Chen", "Alex Rivera"]. All three patterns are real, everyday JavaScript with no regex or manual index tracking required.
When to use indexOf instead
If you need to know where a substring appears, not just whether it appears, use indexOf:
const text = "The quick brown fox";
console.log(text.includes("fox"));
console.log(text.indexOf("fox"));The first prints true. The second prints 16, telling you exactly where "fox" begins within the text. If the substring is not found, indexOf returns -1 instead of false.
One more difference worth knowing: includes, startsWith, and endsWith throw a TypeError if you pass a regular expression as the search argument. Stick to plain string arguments with all four methods.
Common mistakes
Expecting case-insensitive behavior is the most frequent error. The expression "JavaScript".includes("java") always returns false. Normalize casing first when accuracy across cases matters.
Swapping the string and the argument is another common slip. "hello".includes("he") is true because "he" is inside "hello". But "he".includes("hello") is false because the longer string is not inside the shorter one.
Using startsWith when you need a broader check confuses the intent. The expression url.startsWith("rune.codes") against "https://blog.rune.codes/posts" returns false because startsWith only checks the very beginning. Use includes instead when you need to search the entire string.
Rune AI
Key Insights
- includes(substring) returns true if the substring appears anywhere in the string.
- startsWith(substring) returns true only if the string begins with the substring.
- endsWith(substring) returns true only if the string ends with the substring.
- All three accept a second argument: a start position for includes/startsWith, and a length for endsWith.
- All three are case-sensitive. Normalize casing before comparing when needed.
Frequently Asked Questions
Are includes, startsWith, and endsWith case-sensitive?
Can I use these methods with variables?
Do these methods work in all browsers?
Conclusion
includes, startsWith, and endsWith are the cleanest way to check if a string contains a substring in JavaScript. They return a straightforward boolean. Use includes for general substring checks, startsWith for prefixes, and endsWith for suffixes. When you need the exact position instead of a boolean, use indexOf.
More in this topic
Is JavaScript Frontend or Backend? Full Guide
JavaScript is both a frontend and backend language. Learn the difference between browser JavaScript and Node.js, what each is used for, and which one you should learn first.
Learn JavaScript Step by Step Tutorial with Real Examples
Follow a hands-on tutorial that teaches JavaScript by building a real interactive page. Write your first variables, functions, and event listeners with examples you can run.
JavaScript Tutorial: Complete Beginner's Guide to Programming in 2025
A structured learning path for absolute beginners. Learn what to study first, how to practice, and which JavaScript concepts matter most when you are starting from zero.