Code has been added to clipboard!

How to Use the JavaScript Split String Method

Reading time 2 min
Published Aug 10, 2017
Updated Mar 3, 2020

JavaScript Split String: Main Tips

  • JavaScript string.split() method splits a data string into a substrings array and returns the new array.
  • You can specify where the string will be split by choosing a separator.
  • It's also possible to limit the number of substrings to return.

Using string.split()

The JavaScript split string function transforms a specified string into an array of substrings. It does not affect the original string: instead, it generates new ones.

The syntax for JS split string accepts two parameters:

string.split(separator, limit)

Both of them will be explained with examples in the following section.

Example
var string = "What's your name?";
var result = string.split(" ");

In the JavaScript split string example above, the function returns an array of three substrings. You can also see you need to first specify the string that you wish to divide.

Udacity
Pros
  • Simplistic design (no unnecessary information)
  • High-quality courses (even the free ones)
  • Variety of features
Main Features
  • Nanodegree programs
  • Suitable for enterprises
  • Paid certificates of completion
Coursera
Pros
  • Professional service
  • Flexible timetables
  • A variety of features to choose from
Main Features
  • Professional certificates of completion
  • University-level courses
  • Multiple Online degree programs
Datacamp
Pros
  • Great user experience
  • Offers quality content
  • Very transparent with their pricing
Main Features
  • Free certificates of completion
  • Focused on data science skills
  • Flexible learning timetable

Splitting String with a Separator

The separator sets a string which determines the points the original string should be divided in. This parameter is not required. If you skip it, the function will return a single element. The same will happen if no matches for the separator are found.

In the example below, you can see a case when the separator is omitted:

Example
function learnFunction() {
    var string = "What's your name?";
    var result = string.split();
    document.getElementById("learn").innerHTML = result;
}

Now here we choose a single letter (a) as our separator:

Example
function learnFunction() {
    var string = "What's your name?";
    var result = string.split("a");
    document.getElementById("learn").innerHTML = result;
}

Now in this example, an empty string is chosen as the separator (""). Thus, every UTF-16 code unit is separated:

Example
function learnFunction() {
    var string = "What's your name?";
    var result = string.split("");
    document.getElementById("learn").innerHTML = result;
}

Limiting Substring Quantity

limit is also an optional parameter. However, if it is included in the parentheses of JS split string function, the number of splits will be fixed. Thus, the returned array will consist of a specific number of strings.

In the JavaScript split string example below, the result array is limited to two values:

Example
function learnFunction() {
    var string = "What's your name?";
    var res = string.split(" ", 2);
    document.getElementById("learn").innerHTML = res;
}

Note: even if the limit is set, the function might deliver a shorter list if the original string contains fewer matches.

Latest Udacity Coupon Found:

Verified STAFF PICK

75% OFF COURSES

Udacity Black Friday Offer

The best time to save on Udacity courses is now - follow this coupon to access a 75% Udacity Black Friday discount & enjoy learning at a very low cost!

Expiration date: 08/03/2021
3176 People Used
Only 97 Left
Rating
5.0