Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

JavaScript · Function

split

String.prototype.split() searches a string for a separator and returns an array of the substrings between matches. The separator may be a string, regular expression, or custom object with Symbol.split; an optional limit caps the result length.

How it works

Call split() on a string with a separator to search from left to right and create an array of the text between matches. The separator can be a string, a regular expression, or an object providing Symbol.split; an optional limit caps the number of returned entries.

Syntax

split(separator)
split(separator, limit)

Parameters

NameTypeDescription
separatorstring | RegExp | object | undefinedThe pattern defining where splitting occurs; it may be undefined, a string, or an object with a Symbol.split method.
limitnumberAn optional non-negative integer limiting the number of substrings included in the returned array.

Returns

An Array of strings containing the substrings produced by splitting the string.

Key points

  • split() searches for a pattern and divides the string into an ordered array of substrings.
  • The separator accepts undefined, strings, or custom splitter objects.
  • Without a separator, the complete original string is the sole array element.
  • The optional limit restricts how many substrings are returned.
  • A string separator removes each occurrence and uses those positions as boundaries.
  • Regular-expression separators can add captured text to the output array.

Common mistakes

  • Capturing groups in a regular-expression separator become additional array entries, including undefined for unmatched groups.
  • An empty input does not always produce an empty array; with a non-empty separator, the result contains one empty string.
  • A zero limit immediately produces an empty array.

Example: Split a comma-separated string

const input = "red,green,blue";
const colors = input.split(",");
console.log(colors); // ["red", "green", "blue"]

Splits the string at each comma and returns the resulting substrings.

When to use it

Parse separated values

Convert delimited text such as CSV-like input into individual fields.

Pattern-based tokenization

Break text around flexible patterns, such as whitespace or punctuation.

Bounded splitting

Extract only an initial portion of a large separated string.

Interview questions

QWhat does String.prototype.split return and how does the separator work?

split(separator, limit) returns an array of substrings. It searches from left to right, and separator matches divide the string without appearing as ordinary substring entries.

Follow-up: What can an object separator do? If it supplies Symbol.split, split() calls that method to perform the operation.

QWhich separator values are supported?

separator may be undefined, a string, or an object with Symbol.split, such as a regular expression. Omitting it or passing undefined returns an array containing the original string.

Follow-up: What happens when a separator is a regular expression with captures? The captured groups are included in the returned array.

QHow does the limit argument affect the result?

limit is an optional non-negative integer that bounds the number of array entries. Splitting stops once that many entries have been placed, and remaining text is omitted.

Follow-up: What if the input ends before the limit? The array simply contains fewer entries than the limit.

  • String.prototype.match()
  • String.prototype.replace()
  • String.prototype.search()
  • Symbol.split
  • RegExp

Source: String.prototype.split() | MDN Web Docs. Checked 2026-10-15.