JavaScript split() Function: How to Split a String

Formulas & functions · Updated November 22, 2026

Use JavaScript’s split() method to divide a string into an array of smaller strings: call it on the text and pass the separator, such as text.split(',') for comma-separated values. You can also split on spaces, line breaks, or a regular expression, and optionally limit how many pieces the result contains. You need a JavaScript environment and a string to work with; the method does not change the original string.

What You’ll Need

  • A JavaScript environment, such as a browser console, Node.js, or a project with a JavaScript file.
  • A string you want to divide. If your data is not already a string, convert it deliberately before calling split().
  • A clear idea of the separator: for example, a comma, a space, or a line break.

Step-by-Step

  1. Choose the string to split. The method belongs to strings, so start with a string value. For a quick example, assign text to a variable:
    const sentence = 'JavaScript makes web pages interactive';

    If the value comes from a form, an API, or a file, check what it contains before choosing a separator. For example, a list might be separated by commas, while a sentence might be separated by spaces. A number such as 42 does not have a split() method; convert it with String(value) if splitting its text representation is genuinely what you need.

  2. Call split() with the separator. The separator tells JavaScript where to cut the string. The method returns an array containing the parts, and the separator itself is not included in the result:
    const words = sentence.split(' ');
    console.log(words);

    This produces ['JavaScript', 'makes', 'web', 'pages', 'interactive']. Each ordinary space marks a boundary. The original sentence remains unchanged, so you can store the result in a new variable or use it directly in another expression.

  3. Use the exact delimiter in the data. For comma-separated text, pass a comma; for a pipe-delimited string, pass a pipe:
    const colors = 'teal,amber,violet'.split(',');
    const fields = 'north|south|west'.split('|');

    The first result is ['teal', 'amber', 'violet']; the second is ['north', 'south', 'west']. A delimiter must match what is actually in the string. Passing a comma will not split text that uses semicolons, and passing a regular space will not match a tab character.

  4. Save or inspect the returned array. Since split() returns an array, you can access individual items by index, loop over them, or pass them to another array method:
    const path = 'docs/guides/setup'.split('/');
    console.log(path[0]); // 'docs'
    console.log(path.length); // 3

    Indexes start at zero, so path[0] is the first item. To remove extra spaces around comma-separated names, split first and then trim each item:

    const names = 'Mina, Lee,  Omar'.split(',').map(name => name.trim());

    The result is ['Mina', 'Lee', 'Omar']. split() does not automatically trim whitespace from the pieces.

  5. Use a regular expression for more than one possible separator. Put a regular expression between slashes when a single fixed delimiter is not enough. For example, this splits on one or more commas or whitespace characters:
    const items = 'red, blue\tgreen'.split(/[\s,]+/);

    Here, \s matches whitespace and the comma is another option; + groups consecutive matches into one separator. The result is ['red', 'blue', 'green']. This is useful for messy text, but be precise: a broad expression can split at characters you intended to keep.

  6. Add a limit only when you need to cap the result. The second argument is a maximum number of array elements to return:
    const parts = 'one,two,three,four'.split(',', 2);

    This returns ['one', 'two']. The limit is useful when only the first few pieces matter, but it does not mean “split at exactly this many separators and keep the rest together.” For that behavior, split first and join the remaining pieces, or use a parsing approach suited to the data. A limit of zero returns an empty array.

Common Mistakes

  • Expecting split to modify the original. Strings are immutable in JavaScript, and split() returns a separate array. Assign the result if you need to use it later; do not expect the original string variable to become an array.
  • Forgetting that the separator disappears. In 'a-b'.split('-'), the hyphen is not included in either item. If the delimiter itself carries information you need, consider a regular expression with a capturing group or a different parsing method.
  • Assuming whitespace is removed. Splitting on a single space preserves empty pieces from repeated spaces, and spaces next to commas remain in the resulting values. Trim each item when that matches your data-cleaning goal.
  • Using a string where a regular expression is needed. split('|') searches for the literal pipe, which is fine. But if you write split('/'), the slash is also just a literal separator; regular expressions require slash delimiters, as in split(/[|,]/).
  • Treating split as a complete CSV parser. Splitting a CSV row at commas fails when a quoted field contains a comma, such as "Portland, Oregon". For real CSV files, use a CSV parser that understands quoted fields, escaped quotes, and line breaks.

Two edge cases are worth checking. If the separator is not found, the result is a one-item array containing the original string: 'hello'.split(',') gives ['hello']. If the separator occurs at the end, the result includes a trailing empty string: 'a,b,'.split(',') gives ['a', 'b', '']. These empty values can affect array length and later processing.

Tips

  • Use no argument when you want the whole string as one array item. 'hello'.split() returns ['hello']. Passing an empty string is different: 'hello'.split('') divides the text into UTF-16 code units.
  • Use whitespace carefully. split(' ') splits only on ordinary space characters. To handle runs of spaces, tabs, and line breaks as one boundary, try split(/\s+/). If the string can begin or end with whitespace, trim it first to avoid empty items at the edges.
  • Remember that an empty separator is not always a safe way to get user-perceived characters. JavaScript strings use UTF-16. Splitting with '' can separate the two code units that represent some emoji. For Unicode code points, use Array.from(text); for full grapheme clusters, use Intl.Segmenter where available.
  • Inspect the array while developing. Log the result and its length, or check each item with JSON.stringify() to make leading spaces and empty strings visible. This often reveals why a later calculation or display looks wrong.
  • Pick a method that matches the data format. split() is excellent for simple, consistently delimited text. Nested data, quoted CSV, and structured formats such as JSON need their own parser rather than an increasingly complicated separator expression.

Troubleshooting

  • “split is not a function” appears. The value before .split() is probably not a string. Check it with console.log(value, typeof value). If it is a number and you intend to split its text, call String(value).split(...). If it can be null or undefined, decide how those cases should be handled before converting them; blindly converting may produce the unwanted strings 'null' or 'undefined'.
  • The result is one item instead of several. Compare the separator with the actual characters in the input. A string that looks like a space could contain a tab or a non-breaking space. Log the original value, inspect its source, and use a suitable regular expression such as /\s+/ only if splitting on all whitespace is intended.
  • The output contains empty strings. Repeated separators, leading separators, and trailing separators create empty entries. Decide whether those entries are meaningful. If not, filter them out after splitting, for example with split(',').filter(Boolean); note that this also removes any other empty-string values by design.
  • The first output item has an unexpected space. A comma separator leaves spaces intact. Use split(',').map(part => part.trim()) to clean the individual values. Avoid trimming the whole string alone if spaces around every item need to be removed.
  • Emoji or accented text looks damaged after splitting. Avoid split('') for characters a person perceives as one unit. Use Array.from(text) for code points, or Intl.Segmenter for grapheme-aware segmentation. The latter can account for joined emoji and combining marks when supported by the runtime.

Frequently Asked Questions

Does split() change the original string?
No. Strings are immutable, and split() returns a new array. Keep that array in a variable if you need to access its items later.

What happens if I leave out the separator?
The entire string is returned as the sole array element. For example, 'cat dog'.split() produces ['cat dog'].

How do I split on multiple delimiters?
Pass a regular expression containing the alternatives. For example, 'a,b;c'.split(/[,;]/) splits on commas or semicolons and returns ['a', 'b', 'c'].

How can I split text into lines?
Use a regular expression that recognizes common line endings: text.split(/\r?\n/) handles line feed and Windows-style carriage-return-plus-line-feed endings. If the text may use other line separators, inspect its source and choose a pattern that covers them.

Can I split a string into individual letters?
text.split('') works for basic Latin text but can break some emoji and other Unicode characters. Use Array.from(text) for Unicode code points; use Intl.Segmenter when you need grapheme clusters that correspond more closely to visible characters.

Does the limit count separators or results?
It limits the number of elements in the returned array. For example, 'a,b,c'.split(',', 2) returns two items, ['a', 'b']; the remaining text is not automatically kept as a combined final item.

Related

Try it: the Formula Explainer: paste any formula and see each function and reference in plain English.

More in Formulas & functions

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.