Lodash _.startsWith() Method

Beginner
⏱️ 7 min read
📚 Updated: Jul 2026
🎯 6 Examples + 3 Try It
String utilities

What You’ll Learn

By the end of this tutorial, you’ll use Lodash’s _.startsWith() confidently in real JavaScript projects.

01

Core syntax

_.startsWith(s, target)

02

Boolean

true / false

03

Position arg

Search from offset

04

URL checks

http/https guard

05

Case rules

Sensitive by default

06

vs native

String.startsWith()

What Is _.startsWith()?

_.startsWith() checks whether a string begins with a given prefix. It is ideal for input validation, filtering arrays of filenames, and guarding URL or protocol formats.

💡
Beginner tip

Use _.startsWith(url, 'https://') before fetching remote resources—quick protocol validation without regex.

📝 Syntax

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

Syntax Rules

  • string — The string to inspect.
  • target — Prefix substring to find at the start (from position).
  • position — Optional index to begin searching (default 0).
  • Return — Boolean true if prefix matches.
  • Case — Case-sensitive unless you normalize case first.
javascript
import startsWith from "lodash/startsWith";

const str = "Hello, world!";
const ok = startsWith(str, "Hello");
// -> true

⚡ Quick Reference

TaskCode patternResult
Basic_.startsWith(s, 'pre')true/false
At position_.startsWith(s, 'world', 7)true at offset
Case insensitive_.startsWith(_.toLower(s), 'hello')Normalized
URL_.startsWith(url, 'https://')Protocol guard
Filter filesfiles.filter(f => _.startsWith(f, 'app'))Prefix filter
Natives.startsWith('pre')ES2015 equivalent
Returns
boolean

true or false

Default pos
0

Start of string

Case
sensitive

Normalize if needed

Pair
endsWith

Suffix check

🧰 Parameters

string Required

Source string to test.

_.startsWith(path, '/')
target Required

Prefix to match at position.

'https://'
position Optional

Index where match should begin (default 0).

_.startsWith(s, 'x', 2)
return value Boolean

true when prefix matches from position.

-> true

Examples Gallery

Practical _.startsWith() patterns with sample output and interactive Try It Yourself labs.

📚 Getting Started

Test whether a string begins with a prefix.

Example 1 — Basic prefix check

Verify a greeting starts with Hello.

javascript
const str = "Hello, world!";
const prefix = "Hello";
const startsWithPrefix = _.startsWith(str, prefix);

console.log(startsWithPrefix);
// -> true
Try It Yourself

How It Works

Compares characters from position 0; stops at first mismatch.

📚 Practical Patterns

Search from an offset and handle case.

Example 2 — Position parameter

Check for a prefix starting at a specific index.

javascript
const str = "Hello, world!";
const prefix = "world";

const match = _.startsWith(str, prefix, 7);
// position 7 -> "world!..."

console.log(match);
// -> true
Try It Yourself

How It Works

position shifts where the prefix comparison begins.

Example 3 — URL protocol validation

Reject URLs that do not use http or https.

javascript
function isValidUrl(url) {
  return _.startsWith(url, "http://") || _.startsWith(url, "https://");
}

console.log(isValidUrl("https://example.com"));
// -> true
Try It Yourself

How It Works

Combine two startsWith checks for allowed protocols.

Example 4 — Case-insensitive check

Normalize case before comparing prefixes.

javascript
const str = "Hello, World!";
const prefix = "hello";

const ok = _.startsWith(_.toLower(str), _.toLower(prefix));
// -> true

Example 5 — Filter files by prefix

Select filenames that start with a given pattern.

javascript
const files = ["app.js", "index.html", "app.test.js"];
const jsApps = files.filter(function (f) {
  return _.startsWith(f, "app");
});
// -> ["app.js", "app.test.js"]

📚 Beyond the Basics

Native alternative.

Example 6 — Native String.startsWith()

ES2015 provides the same API on string primitives.

javascript
const s = "Hello";

console.log(_.startsWith(s, "He"));
console.log(s.startsWith("He"));
// both -> true

How It Works

Use Lodash when coercing nullish values or staying consistent in a lodash-heavy codebase.

📋 Related operations

Topic_.startsWithString.startsWith_.endsWith
ChecksPrefix at startPrefix at startSuffix at end
positionSupportedSupportedSupported
Returnbooleanbooleanboolean
null stringLodash coercesThrows on nullLodash coerces

🧠 How _.startsWith() Works

1

Read string

Coerce null/undefined to empty string if needed.

Input
2

Set position

Default 0 or custom offset for comparison.

Offset
3

Compare chars

Match target characters sequentially from position.

Match
=

Return bool

true on full prefix match; false otherwise.

📝 Notes

  • Comparison is case-sensitive—normalize with _.toLower() when needed.
  • Use position to test prefixes mid-string, not only at index 0.
  • For suffix checks, use _.endsWith().
  • Returns false when target is longer than the remaining substring.
  • Validate types in public APIs before relying on coercion.

Conclusion

_.startsWith() is a practical Lodash string helper. Use the patterns above in your projects and explore the next method in the series.

❓ Frequently Asked Questions

It returns true if string begins with target at position (default 0). Otherwise false.
Behavior is equivalent for strings. Lodash provides consistent handling when string may be null/undefined (coerced to empty string).
Index in string where the search for target begins. Useful to check prefixes after a known offset.
Yes by default. Compare lowercased copies with _.toLower() for case-insensitive checks.
Returns false if target is longer than the remaining substring from position.
Use _.endsWith() for suffix checks; startsWith checks the beginning only.
Did you know?

_.startsWith() returns a boolean—use it for guards like protocol checks (https://) or filtering filenames by extension prefix.

Practice _.startsWith() in the Live Editor

Open the Try It editor and run the examples from this tutorial.

Open Try It editor →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful