Skip to main content

startsWith()

startsWith(str, prefix, position): boolean

Checks if a string starts with a given prefix.

DEPRECATED

Use string.startsWith() directly instead.


Parametersโ€‹

str: string | null | undefinedโ€‹

The string to check.

prefix: string | null | undefinedโ€‹

The prefix to search for.

position: number = 0โ€‹

The position to start searching from.


Returns: booleanโ€‹

true if the string starts with the prefix, false otherwise.


See Alsoโ€‹


Sinceโ€‹

2.0.0


Also known asโ€‹

startsWith (Lodash, es-toolkit, Remeda, Ramda, Effect) ยท โŒ (Radashi, Modern Dash, Antfu)


Exampleโ€‹

// โŒ Deprecated approach
startsWith('hello world', 'hello'); // => true
startsWith('hello world', 'world', 6); // => true

// โœ… Recommended approach
'hello world'.startsWith('hello'); // => true
'hello world'.startsWith('world', 6); // => true

How it works?โ€‹

Checks if string starts with the given target. Deprecated: Use string.startsWith() directly (ES2015).

Native Equivalentโ€‹

// โŒ startsWith('hello', 'he')
// โœ… 'hello'.startsWith('he')

Use Casesโ€‹

Check prefix ๐Ÿ“Œโ€‹

Check if string starts with value.

'hello world'.startsWith('hello');  // true

Validate protocolโ€‹

Check URL protocol.

const isSecure = url.startsWith('https://');