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://');