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.

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.

What Is JavaScript Used For?
Tutorial
Introduction
Output
Syntax
Comment
Commands
Operators
Comparison and Logical Operators
Data Types
Math.random()
Type Conversion
Function Definitions
Events
Objects
Object Properties
Prototype
Array
Sorting Arrays
Strings
Numbers
Number Format
Math Object
Onclick Event
Date
Date Formats
Scope
Regular Expressions
Reserved Words
Common Mistakes
Performance
Forms
Form Validation
Window: The Browser Object Model
Popup Boxes
Cookies
JSON
AJAX Introduction
AJAX Form
Automatic File Download
Functions
Array Methods
String Methods
Date Methods
Timing Events
Cheat Sheet
JavaScript in HTML
HTML DOM Methods
HTML DOM Changing HTML
HTML DOM Animation
HTML DOM EventListener
HTML DOM Navigation
HTML DOM NodeList
HTML DOM Element Nodes
Array Functions
Boolean
Calling a Function
Date Functions
Global Objects
Input Text
Operator
Statements
String Functions
Math
Math.random
Number
RegEx
alert
array.filter
array.length
array.map
array.reduce
array.push
array.sort
break and continue
className
confirm
decodeURIComponent
for
forEach
if
indexOf
innerHTML
location.reload
number.toString
onclick
onload
parseInt
prompt
replace
setAttribute
setInterval
setTimeout
slice
splice
string.includes
string.indexOf
string.split
style.display
submit
substr
substring
switch
test
throw, try and catch
toLowerCase
toUpperCase
use strict
while
window.history
window.location
window.navigator
window.screen