String.prototype.startsWith()

A prefix test that says what it means. It replaces both indexOf(x) === 0 and the anchored regex, and unlike the regex it needs no escaping.

String methodES2015Live demo
Common call
url.startsWith('https://')
Returns
boolean
Replaces
s.indexOf(x) === 0 and /^x/.test(s)
Watch out
case sensitive; a RegExp argument throws
string.startsWith(searchString[, position])
→ boolean

Demo

Live evaluation
Try:
Inputs
sstringthe string to test
searchstringthe prefix
Output
'https://x.com'.startsWith('https://')
true

The first two cases are the everyday use — distinguishing https from http is a prefix test, and writing it this way is both clearer and safer than a regex where the slashes and dots would need escaping. The fourth case shows the difference from includes: 'ell' is present in 'hello' but not at the beginning, so this returns false where includes would return true. As with includes, an empty search string is always true.

Parameters

NameTypeRequiredDescription
searchStringstringyesThe prefix to test for. A RegExp is a TypeError, as with includes and endsWith.
positionnumberno (0)Treat the string as if it began here. startsWith("el", 1) tests the substring starting at index 1.

Return value

boolean — True if the string begins with the search text — or begins with it at position, when one is given.

Common patterns

Check a protocol or scheme
The most common real use.
if (!url.startsWith('https://')) throw new Error('insecure');
Match any of several prefixes
some over a list beats a chain of ORs.
const isInternal = PREFIXES.some(p => path.startsWith(p));
Strip a known prefix
Test, then slice — or use replace for the modern form.
const rest = s.startsWith(p) ? s.slice(p.length) : s;

Examples

1. Matching prefix
'hello'.startsWith('he')
Returns
true
2. Not a prefix
'hello'.startsWith('ell')
Returns
false
3. includes differs
'hello'.includes('ell')
Returns
true
4. From a position
'hello'.startsWith('el', 1)
Returns
true
5. Empty is true
'hello'.startsWith('')
Returns
true
6. RegExp throws
'abc'.startsWith(/a/)
Returns
TypeError: First argument to String.prototype.startsWith must not be a regular expression

Pitfalls

1. Case sensitive, with no option
Same as includes — there is no flag. Protocol and header checks against text of unknown origin should lowercase first, since HTTP schemes and header names are case-insensitive by specification.
Misses it
'HTTPS://x'.startsWith('https://')
false
Fold first
'HTTPS://x'.toLowerCase().startsWith('https://')
true
2. A prefix test is not a URL check
startsWith('https://') says nothing about what follows. It is a reasonable guard against obviously wrong input and no defence at all against a crafted URL — parse with the URL constructor when the answer matters for security.
Passes anything
'https://evil.com/?x=bank.com'.startsWith('https://')
true
Parse it
new URL(u).protocol === 'https:'
checks the real scheme
3. A RegExp argument throws
Identical to includes and endsWith: these three refuse regexes rather than stringifying them. Use an anchored pattern with test if you need one.
Throws
'abc'.startsWith(/a/)
TypeError: First argument to String.prototype.startsWith must not be a regular expression
Anchored test
/^a/.test('abc')
true
4. The position argument shifts the whole test
It does not mean "search from here onwards" — it re-anchors the test at that index. startsWith(x, 3) is true only if x begins exactly at index 3, which is easy to misread as an offset search.
Not a search
'hello'.startsWith('lo', 1)
false
Anchored at 3
'hello'.startsWith('lo', 3)
true

When to use

Use it
  • Testing a protocol, scheme or known prefix
  • Routing on a path prefix
  • Detecting a marker at the beginning of a line
  • Anywhere indexOf(x) === 0 appears
Reach for something else
  • The match may be anywhere → includes
  • You are testing the end → endsWith
  • You need a real pattern → an anchored RegExp with test
  • Security decisions about URLs → the URL constructor

Notes

Complexity
O(m) in the length of the prefix — it stops at the first mismatch
Return
A boolean; nothing is allocated
CPython impl
V8: Builtins-string-startswith
Memory
No allocation
Thread-safe
Single-threaded; the string is only read

FAQ

startsWith for literal text. It needs no escaping, which matters when the prefix is a variable — a path or URL full of dots and slashes becomes a minefield inside a RegExp. Keep the anchored regex for genuine patterns.

s.startsWith(prefix);                     // safe
new RegExp('^' + prefix).test(s);         // needs escaping

History

ES2015
Added with includes and endsWith as part of the string-method additions.