Lodash _.split() 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 _.split() confidently in real JavaScript projects.

01

Core syntax

_.split(str, sep, limit)

02

Safe input

null/undefined handling

03

CSV parsing

Comma-separated rows

04

Limit splits

Cap segment count

05

Regex sep

Flexible delimiters

06

vs native

String.split() compare

What Is _.split()?

_.split() breaks a string into an array of substrings at each occurrence of a separator. It wraps the familiar native split behavior with Lodash’s predictable edge-case handling.

💡
Beginner tip

Use _.split('a,b,c', ',') when you want a guaranteed array even if the source string might be missing—Lodash coerces safely.

📝 Syntax

javascript
_.split(string, separator, [limit])

Syntax Rules

  • string — The string to split (null/undefined become empty string).
  • separator — String or RegExp delimiter; omit for per-character split.
  • limit — Optional max number of splits (remaining text stays in last element).
  • Return value — Array of substring segments.
  • Immutable — Original string is unchanged.
javascript
import split from "lodash/split";

const originalString = "apple,banana,orange";
const fruits = split(originalString, ",");
// -> ["apple", "banana", "orange"]

⚡ Quick Reference

TaskCode patternResult
Comma list_.split(s, ',')['a','b','c']
With limit_.split(s, ',', 2)First 2 splits only
Trim tokens_.map(_.split(s, ','), _.trim)Clean CSV cells
Regex_.split(s, /\s+/)Whitespace tokens
Lines_.split(text, '\n')Row array
Natives.split(',')Same for valid strings
Mutates?
No

New array returned

Separator
str|RegExp

Flexible delimiters

Limit
optional

Cap split count

Pair with
_.trim

Clean segments

🧰 Parameters

string Required

Source string to split. null/undefined coerce to empty string.

_.split(csv, ',')
separator Required*

Delimiter string or RegExp. *Omit for char-by-char split.

',' or /\s+/
limit Optional

Maximum number of splits to perform.

_.split(s, ',', 3)
return value Array

Segments in order; may include empty strings between delimiters.

['a','b']

Examples Gallery

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

📚 Getting Started

Split simple delimited strings into arrays.

Example 1 — Comma-separated list

Split a grocery list string into individual fruit names.

javascript
const originalString = "apple,banana,orange";
const splitArray = _.split(originalString, ",");

console.log(splitArray);
// -> ["apple", "banana", "orange"]
Try It Yourself

How It Works

Each comma becomes a split point; segments keep their original casing.

📚 Practical Patterns

Clean tokens and control split counts.

Example 2 — Trim whitespace after split

Parse sloppy comma-separated input with extra spaces.

javascript
const messy = "apple, banana, orange";
const trimmed = _.map(_.split(messy, ","), _.trim);

console.log(trimmed);
// -> ["apple", "banana", "orange"]
Try It Yourself

How It Works

Split first, then _.trim each segment for consistent tokens.

Example 3 — Limit the number of splits

Keep remainder in the last segment when you only need two fields.

javascript
const longString = "apple,banana,orange,grape,mango";
const limited = _.split(longString, ",", 3);

console.log(limited);
// -> ["apple", "banana", "orange,grape,mango"]
Try It Yourself

How It Works

limit stops after N-1 separators; leftover text joins the final element.

Example 4 — Parse CSV row

Turn one CSV line into an array of cell values.

javascript
const row = "John,Doe,30";
const cells = _.split(row, ",");
// -> ["John", "Doe", "30"]

Example 5 — Regex separator

Split on multiple operators in an expression.

javascript
const expression = "x + y - 10 * z";
const tokens = _.split(expression, /[\s+*\-]/);
// includes empty strings between operators

📚 Beyond the Basics

When native split is enough.

Example 6 — Native String.split() comparison

For guaranteed non-null strings, native split is equivalent.

javascript
const s = "a|b|c";

console.log(_.split(s, "|"));
console.log(s.split("|"));
// both -> ["a", "b", "c"]

How It Works

Choose _.split when input may be null/undefined or you already use Lodash pipelines.

📋 Related operations

Topic_.split()String.split()_.words()
OutputArray of segmentsArray of segmentsWord tokens only
null inputCoerces to ''Throws on nullHandles via lodash
Separatorstr / RegExpstr / RegExpLodash word rules
Best forDelimited dataSimple known stringsNatural language words

🧠 How _.split() Works

1

Coerce input

null/undefined become empty string for safe splitting.

Input
2

Find separator

Scan for string or RegExp delimiter matches.

Match
3

Slice segments

Extract substrings between matches into an array.

Split
=

Apply limit

Stop early if limit is set; merge tail into last item.

📝 Notes

  • RegExp splits may produce empty strings between consecutive delimiters.
  • Use _.map(_.split(...), _.trim) for user-entered CSV.
  • For word tokenization (not delimiter split), see _.words().
  • The limit parameter behaves like native String.split.
  • Strings are immutable—_.split() never changes the source.

Conclusion

_.split() 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 splits a string into an array of substrings using a separator (string or RegExp), with an optional limit on the number of splits.
Behavior matches String.split for valid strings. Lodash adds consistent handling when string is null or undefined (treated as empty string).
Yes. Pass a RegExp like /[,\s]+/ to split on commas or whitespace, same as native split.
It caps how many splits occur. Remaining text stays in the final array element.
No. Strings are immutable; a new array is returned.
When parsing user CSV or space-separated input, map with _.trim on each segment for clean tokens.
Did you know?

_.split() mirrors native String.prototype.split but handles null/undefined inputs safely by coercing to an empty string.

Practice _.split() 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