JavaScript String split() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Method

Definition

String.prototype.split(separator, limit?) divides a string into an array of substrings wherever separator matches. The original string is unchanged. Pair with Array.join() to put pieces back together.

Pass the required arguments shown below. Check return values and browser support before you rely on the result.

Syntax

Call string.split. Start with this line.

JavaScript
str.split(separator, limit?)
CallWhat it does
str.split(separator, limit?)Primary signature.
string.split(...)Call with the arguments your case needs.

Built into JavaScript. Call split(...) on a string (or String instance) — no import required.

Library declarations
JavaScript
str.split(separator, limit?)

Parameters

ParameterDescription
separatorOptional. String or RegExp (omit → whole string in one element)
limitOptional. Max number of pieces in the result

Return Value

Return ValueDescription
*Array of substrings

JavaScript Examples

Example 1 — Split by a Separator

Cut on a delimiter, or into characters with an empty string.

JavaScript
const csv = "a,b,c";

console.log(csv.split(","));  // ["a", "b", "c"]
console.log("hello".split("")); // ["h", "e", "l", "l", "o"]
console.log("hi".split());    // ["hi"]
Try It Yourself

How It Works

  1. Pick a delimiter. split(",") cuts wherever a comma appears.
  2. Empty separator. split("") yields one entry per UTF-16 code unit.
  3. Omit separator. split() returns a one-element array with the whole string.
  4. Source unchanged. csv is still "a,b,c".

Example 2 — Limit the Number of Pieces

Stop after a fixed count — leftover text is discarded.

JavaScript
console.log("a-b-c-d".split("-", 2)); // ["a", "b"]
console.log("a-b-c-d".split("-", 1)); // ["a"]
console.log("a-b-c-d".split("-", 0)); // []
Try It Yourself

How It Works

  1. Cap the array. limit of 2 keeps only the first two pieces.
  2. Rest is dropped. "c-d" is not included after the limit is reached.
  3. Limit 1. Useful for “first token only” parsing.
  4. Limit 0. Always returns an empty array [].

Example 3 — Regex Separator & Empty Fields

Flexible delimiters, and what consecutive separators leave behind.

JavaScript
console.log("one  two   three".split(/\s+/)); // ["one", "two", "three"]
console.log("a,,b".split(","));               // ["a", "", "b"]
console.log("Hello 1 word.".split(/(\d)/));   // ["Hello ", "1", " word."]
Try It Yourself

How It Works

  1. Collapse whitespace. /\s+/ treats one or more spaces as one cut.
  2. Empty pieces. Two commas in a row put "" between them.
  3. Capturing groups. (\d) inserts the matched digit into the array.
  4. Pick the tool. Use a string for fixed delimiters; use regex when the separator varies.

Browser & Runtime Support

String.prototype.split() has been in JavaScript since the early language and is universal across browsers and runtimes.

✓ Baseline · Universal

String.prototype.split()

Supported in all modern browsers, Internet Explorer, and every current Node.js release. Safe everywhere.

100% Browser support
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
String.split() Universal

Bottom line: Use confidently in any environment. Pair with Array.join() to rebuild strings, and prefer regex separators when delimiters vary.

Frequently Asked Questions

String.prototype.split(separator, limit?) divides a string into an ordered list of substrings and returns them in an Array. The original string is unchanged.
Omitting separator (or passing undefined) returns a one-element array containing the whole string — for example "hi".split() is ["hi"].
An empty-string separator splits into UTF-16 code units (roughly “characters” for BMP text). Be careful with emoji — surrogate pairs can break. Prefer Array.from(str) or [...str] for Unicode code points.
limit is a non-negative integer capping how many pieces go into the array. Leftover text is discarded. limit 0 returns [].
Yes. Pass a RegExp as separator. If the regex has capturing groups, those captures are included in the result array.
Use Array.prototype.join(). For example "a,b,c".split(",").join("-") returns "a-b-c".

Next: startsWith()

Learn how to check whether a string begins with a given prefix.

startsWith() tutorial →

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.

18 people found this page helpful