search

String.prototype.startsWith

Returns true if the string begins with searchString at the given position (default 0). Added in ES2015 and is more efficient than slice() + === comparisons.

str.startsWith(searchString[, position])

Parameters

Parameter Purpose
searchString Prefix to test against
position Index to treat as the start of the string (default 0)

Examples

console.log('https://example.com'.startsWith('https://'));

Prints true

console.log('hello world'.startsWith('world', 6));

Prints true

console.log('Hello'.startsWith('hello'));

Prints false (case-sensitive)

console.log('abc'.startsWith(''));

Prints true

Gotcha

Throws TypeError if searchString is a RegExp. Case-sensitive — normalize casing beforehand if you need loose matching.

Related methods

← All JS string methods · JS array methods