JavaScript - String split() function

The split() function separates an original string into an array of substrings.

The split() method divides a String using a specified separator into an ordered set of substrings, returning a resultant array.

The division is performed on provided search pattern i.e separator; provided as the first parameter in the method’s call.


Syntax

str.split([separator[, limit]]);

Parameters

separator (optional)

The separator is a pattern describing where each split should occur. It can be a simple string or a regular expression.

  • A separator parameter having a single character is the simplest one. For example, a string containing comma-separated values (CSV) could be parsed by passing a comma(,) character as the separator, such as csvString.split(",").
  • The separator can also contain multiple characters, so to split the string that the entire character sequence must match.
  • If a separator is an empty string (""), then the resultant array will have each of the string’s UTF-16 “characters” as a single element.

limit (optional)

  • A non-negative integer limiting the number of elements. Specifying the limit returns the number of elements after splitting the string. The leftover text is not included in the array at all.
  • The array may contain fewer entries than the limit; if the end of the string is reached before the limit is reached.
  • If the limit is reached before reaching the end of the string then the resultant array will have fewer elements.
  • If the end of the string is reached before reaching then limit then the resultant array will have all elements after splitting the string
  • If the limit is 0, no splitting is performed.
  • If a negative integer is given as limit then all elements after splitting are returned i.e negative will be ignored

Example 1

let quote = "An ounce of practice is worth a thousand words.";
quote.split(" ");

// Output
// ["An", "ounce", "of", "practice", "is", "worth", "a", "thousand", "words."]

Example 2

// Namaste 🙏 means "bowing to you"
let greet = "Namaste";
greet.split("");

// Output
// ["N", "a", "m", "a", "s", "t", "e"]

In the above example string split function will split the original string into individual characters.

If you do not specify any separator character like split(""), then split function will return an array of all individual characters.

Example 3

let quote = "An ounce of practice is worth a thousand words.";
quote.split(" ", 2);

// Output
// ["An", "ounce"]

If you specify limit character, then split function will return a specified number of elements in the resultant array.

In the above example, only "An" and "ounce" are returned by the split function, as 2 is passed as the limit value.

Example 4

"1+2+3+4+5+6+7+8+9".split("+");

// Output
// ["1", "2", "3", "4", "5", "6", "7", "8", "9"]

In the above example string split function can also be directly used with string. In case you might not want to declare a variable.

Example 5

"1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9".split(" + ", 4);

// Output
// ["1", "2", "3", "4"]

Also, string separator does not have to be a single character, it can be any multiple or combination of characters.

In the above example separator " + " (whitespace, plus, whitespace) and limit 4 will split string with a separator having multiple characters and returns only 4 elements at the last.

Common uses of split() function

  • Creating or extracting an array of words from a sentence:
let sentence = "A baby is born with a need to be loved and never outgrows it.";
sentence.split(" ");

// Output
/*
    ["A", "baby", "is", "born", "with", "a", "need", "to", 
     "be", "loved", "and", "never", "outgrows", "it."]
*/
  • Reversing the letters in a string or word:
let word = "rehtoM";
word.split("").reverse().join("");
"Mother";

Hope you learn something new. If this article was helpful, share it.

Happy coding

Vishnu Damwala
Vishnu Damwala

A web geek, an industry experienced web developer & tutor/instructor residing in India 🇮🇳