String.prototype.startsWith() is a case-sensitive prefix check that returns true or false (same API as MDN String.prototype.startsWith()). Learn the optional position, empty-string behavior, why regex is not allowed, how it compares to endsWith() and includes(), five examples, and try-it labs.
01
Kind
Instance method
02
Returns
true / false
03
Case
Sensitive
04
Regex?
No (TypeError)
05
Mutates?
No
06
Baseline
Widely available
Fundamentals
Introduction
Need to know whether a string begins with a certain prefix — a protocol, a filename stem, or a command keyword? startsWith() answers with a clear boolean.
The check is always case-sensitive. An optional position lets you treat a later index as the new “start.” Regular expressions are not allowed — pass a string (or a value that coerces to a string).
💡
Beginner tip
Prefer str.startsWith("https://") over str.indexOf("https://") === 0 — it reads exactly what you mean.
Without a position, the check starts at index 0. With position = 3, the string is treated as starting at "urday...", so "Sat" no longer matches.
Example 2 — Prefix from a Position
MDN demo: Hamlet line — find a phrase that is not at index 0.
JavaScript
const str = "To be, or not to be, that is the question.";
str.startsWith("To be"); // true
str.startsWith("not to be"); // false
str.startsWith("not to be", 10); // true
Prefix checks keep validation readable. Pair with endsWith when you care about the other end of the string.
Applications
🚀 Common Use Cases
URL / protocol checks — startsWith("https://").
Command routing — detect prefixes like "/help" or "npm ".
Path filters — keep files under a folder prefix.
Feature flags / tags — strings that begin with a known marker.
Not for “contains” — use includes() when the text can appear anywhere.
Not for regex prefixes — use /^pattern/ with test().
🧠 How startsWith() Decides
1
Read searchString / position
Default position to 0; reject regex with TypeError.
Input
2
Compare the prefix
Check characters from position for an exact match.
Compare
3
Handle empty search
Empty searchString counts as a successful match.
Edge
4
✅
Return true or false
Original string is never modified.
Important
📝 Notes
The search is case-sensitive.
position sets where the prefix must start — it does not scan forward.
Empty searchString always returns true.
Passing a regex throws TypeError.
Omitting searchString searches for the literal "undefined".
Pair with endsWith() for suffix checks and includes() for “anywhere.”
Compatibility
Browser & Runtime Support
String.prototype.startsWith() is Baseline Widely available — supported across modern browsers since September 2015.
✓ Baseline · Widely available
String.startsWith()
Safe for production in browsers and runtimes (Node.js, Deno, Bun). Prefer startsWith() for readable prefix checks.
UniversalWidely available
Google ChromeSupported · Desktop & Mobile
Full support
Mozilla FirefoxSupported · Desktop & Mobile
Full support
Apple SafariSupported · macOS & iOS
Full support
Microsoft EdgeSupported · Chromium
Full support
Internet ExplorerNo native support · Use a polyfill
Polyfill
OperaSupported · 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
startsWith()Excellent
Bottom line: Use String.startsWith() to test prefixes. Remember it is case-sensitive, and regex arguments throw TypeError.
Wrap Up
Conclusion
String.prototype.startsWith() is the clearest way to ask “does this string begin with …?” — with an optional position for mid-string prefixes. Keep it for literal strings; reach for regex APIs when you need patterns.
Pass an explicit string — avoid accidental undefined
Combine with endsWith for file-type logic
Use position when the “start” is mid-string
❌ Don’t
Pass a regex to startsWith
Assume case-insensitive matching
Use indexOf(...) === 0 when startsWith is clearer
Expect position to search later occurrences
Forget empty search strings always match
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about String.startsWith()
Boolean prefix check — case-sensitive, no regex.
5
Core concepts
📝01
Returns
boolean
API
🔍02
Checks
prefix
Role
🔢03
Case
sensitive
Rule
❌04
Regex
TypeError
Limit
⚡05
Mutates
no
Immutable
❓ Frequently Asked Questions
String.prototype.startsWith(searchString, position?) returns true if the string begins with searchString (optionally checked from position), otherwise false. The search is case-sensitive.
Yes. "Hello".startsWith("hello") is false. Normalize both sides with toLowerCase() when you need a case-insensitive prefix check.
No. If searchString is a RegExp, startsWith() throws TypeError. Use RegExp.test() or String.search() for pattern checks.
true for any string. An empty search string is treated as a match at the start.
position is the index where the prefix is expected to begin (default 0). For example "abcdef".startsWith("de", 3) is true because indexes 3–4 are "de".
startsWith() only checks the beginning (or from position as a new start). includes() looks for the substring anywhere in the remaining text.
Did you know?
startsWith, endsWith, and includes all reject regex arguments with TypeError — a deliberate design so boolean substring helpers stay simple. For patterns at the start of a string, use /^.../ with test().