Skip to content

String.startsWith method

String › startsWith

Official

Checks if the string starts with a specific substring.

Signature:

startsWith(searchString: string, position?: number | undefined): boolean

Parameters:

Parameter Type Description
searchString string The substring to check for.
position? number | undefined The position to start checking from.

Returns: booleantrue if the string starts with the substring, false otherwise.

Example:

console.log('foo'.startsWith('fo')); // true
console.log('foo'.startsWith('oo')); // false
console.log('foo'.startsWith('foo', 1)); // false
console.log('foo'.startsWith('oo', 1)); // true