Lodash startsWith: Checking Prefixes at Offsets

This article explores how the Lodash utility function _.startsWith inspects strings to determine if they begin with a specific target substring, focusing particularly on the optional offset parameter. You will learn the function's syntax, the mechanics of how offsets shift the search index, how edge cases such as negative or out-of-bounds positions are handled, and how it compares to standard JavaScript string methods.

Syntax and Parameters

The _.startsWith method in Lodash determines if a string starts with a given target string. Its signature is:

_.startsWith([string=''], [target], [position=0])

The method returns true if target matches the substring starting at position, and false otherwise.

How the Offset Works

When a position argument is provided, _.startsWith does not evaluate the string from index 0. Instead, it shifts the comparison window so that the character at the specified offset acts as the starting point of the string.

Internally, the check operates similarly to extracting a slice of the string from the specified index and checking if that slice begins with the target:

const text = 'error: invalid input detected';

// Without offset: checks from index 0
_.startsWith(text, 'invalid'); 
// => false

// With offset: checks from index 7
_.startsWith(text, 'invalid', 7); 
// => true

In this example, index 7 corresponds to the character 'i' in 'invalid input detected'. Because the substring at this offset immediately matches 'invalid', the function evaluates to true.

Handling Edge Cases in Offsets

Lodash includes built-in safeguards when handling unusual position values:

1. Negative Offsets

If a negative integer is passed as the position, Lodash clamps the value to 0 rather than counting backward from the end of the string.

const text = 'production-build';

_.startsWith(text, 'prod', -5);
// => true (treated as offset 0)

2. Out-of-Bounds Offsets

If the position exceeds the length of the string, Lodash cannot find a match unless the target is an empty string ''.

const text = 'alpha';

_.startsWith(text, 'a', 10);
// => false

_.startsWith(text, '', 10);
// => true

3. Non-Integer Positions

If a floating-point number is supplied as the offset, Lodash converts it to an integer using standard truncation rules (similar to Math.floor for positive values).

const text = 'stage-deploy';

_.startsWith(text, 'deploy', 6.8);
// => true (evaluates at index 6)

Difference from Native String.prototype.startsWith

Modern JavaScript includes a native String.prototype.startsWith(searchString, position) method that behaves similarly to Lodash's implementation. However, Lodash's _.startsWith provides safety against null or undefined references:

let missingString = null;

// Native implementation throws a TypeError:
// missingString.startsWith('test', 0); // TypeError: Cannot read properties of null

// Lodash safely returns false:
_.startsWith(missingString, 'test', 0); 
// => false

By safely coercing nullish values to empty strings and clamping negative offsets to 0, Lodash ensures defensive and predictable prefix matching across any string offset.