Encodera

Thanks for reading!

Donate to the site — we work hard for you.

JavaScript reference

String.prototype.startsWith

Checks whether a string starts with a given substring.

Available since: ES2015

Syntax

str.startsWith(searchString, position)

Parameters

ParameterTypeDescription
searchStringstringThe expected beginning of the string.
positionnumberOptional. The position where the check starts; defaults to 0.

Return value

boolean`true` if the string starts with the substring, `false` otherwise.

Examples

Checking the protocol of a URL:

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

Output:

true
false

The companion endsWith() checks the end of a string:

console.log('photo.png'.endsWith('.png'));

Output:

true

Notes

The comparison is case-sensitive.

See also

Related recipes

In the tutorial

  • String methods — Useful string methods in JavaScript: finding a position, reading characters, padStart, repeat, split and join, method chains.

Thanks for reading!

Donate to the site — we work hard for you.