String.prototype.startsWith()

Determines whether a string begins with the given substring.

Since ES2015 Spec ↗

Syntax

str.startsWith(searchString, position)

Parameters

NameTypeRequiredDescription
searchString string Yes The substring to test for at the start.
position number No Position in the string at which to begin the check. Defaults to 0.

Returns

boolean — true if the string starts with the substring; otherwise false.

Examples

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

Notes

Case-sensitive. Throws TypeError if given a regular expression. The optional position lets you test starting from an offset.

Browser & runtime support

EnvironmentSince version
chrome 41
firefox 17
safari 9
edge 12
node 4.0

See also