The Wayback Machine - https://web.archive.org/web/20240812233817/https://www.geeksforgeeks.org/javascript-string-split-method/
Open In App

JavaScript String split() Method

Last Updated : 26 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

JavaScript split() method proves invaluable for breaking down strings into arrays of substrings, utilizing a designated separator as a reference point.

This method breaks the original string into segments wherever the separator occurs, generating an array where each element corresponds to a distinct substring. By using split(), developers gain a powerful tool for parsing and organizing string data according to specific delimiters or patterns.

Syntax: 

str.split(separator, limit);

Parameters:

  • separator: It is used to specify the character, or the regular expression, to use for splitting the string. If the separator is unspecified then the entire string becomes one single array element. The same also happens when the separator is not present in the string. If the separator is an empty string (“”) then every character of the string is separated.
  • limit: Defines the upper limit on the number of splits to be found in the given string. If the string remains unchecked after the limit is reached then it is not reported in the array.

Return value:

This function returns an array of strings that is formed after splitting the given string at each point where the separator occurs. 

Example: Here is the basic example of the split() method.

JavaScript
// JavaScript Program to illustrate split() function 

function func() {
    //Original string 
    let str = 'Geeks for Geeks'
    let array = str.split("for");
    console.log(array);
}
func(); 

Output
[ 'Geeks ', ' Geeks' ]

Explanation:

  • JavaScript program defines function func() splitting string ‘Geeks for Geeks’ using “for” as separator.
  • Resulting array [“Geeks “, ” Geeks”] is logged to console.

Example: Here, the function split() creates an array of strings by splitting str wherever ” ” occurs.

JavaScript
// JavaScript Program to illustrate split() function

function func() {
    //Original string
    let str = 'It iS a 5r&e@@t Day.'
    let array = str.split(" ");
    console.log(array);
}
func();

Output
[ 'It', 'iS', 'a', '5r&e@@t', 'Day.' ]

Explanation:

The string ‘It iS a 5r&e@@t Day.’ is split into an array using a space as the separator.

  • Resulting array: [“It”, “iS”, “a”, “5r&e@@t”, “Day.”].

We have a complete list of Javascript string methods, to check those please go through this Javascript String Complete reference article.

Supported Browsers:

JavaScript String Split() methods- FAQs

What is the split() method in JavaScript, and what is its primary use?

The split() method in JavaScript is used to divide a string into an array of substrings, using a specified separator as the reference point. This method is valuable for parsing and organizing string data according to specific delimiters or patterns. When a string is split, each element in the resulting array corresponds to a distinct substring separated by the specified delimiter.

How does the separator parameter affect the behavior of the split() method?

The separator parameter in the split() method determines where the string will be divided. It can be a character, a substring, or a regular expression. If the separator is not specified, the entire string is returned as a single array element. If the `separator` is not found in the string, the whole string is also returned as a single element in the array.

What is the purpose of the limit parameter in the split() method?

The limit parameter in the split() method defines the maximum number of splits to be performed on the string. Once the limit is reached, any remaining part of the string is not included in the resulting array. This allows developers to control the number of substrings created, which can be useful when only a certain number of segments are needed.

What is the return value of the split() method, and how is it structured?

The split() method returns an array of strings. Each element of this array is a substring of the original string, divided at each point where the specified separator occurs. The structure of the array depends on the occurrences of the separator and the specified limit.

How does the split() method handle cases where the separator is not found in the string?

If the specified separator is not found within the string, the split() method returns an array containing a single element, which is the entire original string. This ensures that the method always returns an array, regardless of whether any splits are made.


Previous Article
Next Article

Similar Reads

JavaScript | Split a string with multiple separators
Given a statement which contains the string and separators, the task is to split the string into substring.String split() Method: The str.split() function is used to split the given string into array of strings by separating it into substrings using a specified separator provided in the argument. Syntax: str.split(separator, limit) Parameters: This
3 min read
How to split multiline string into an array of lines in JavaScript ?
Multiline string in JavaScript means a string having two or more lines. To split a multiline string into an array we need to use split() in our JavaScript code. JavaScript split(separator, limit) Method: The split() function is used to split the data depending upon the attributes we are passing in it. The separator attributes specify that from this
2 min read
JavaScript Program to Split a String into a Number of Sub-Strings
Given a string, the task is to split the string into number of sub-strings using JavaScript. This task is common in scenarios where data needs to be segmented or processed in chunks. This article provides a flexible solution that can divide a given string into a desired number of smaller substrings, allowing for efficient manipulation or analysis o
2 min read
JavaScript String.Split() Example with RegEx
The String.split() method in JavaScript is used to split a string into an array of substrings based on a specified delimiter. When a regular expression (RegEx) is used as the delimiter, it allows for more complex and powerful splitting criteria. This is particularly useful for splitting strings based on patterns rather than static characters. By le
2 min read
JavaScript Program to Check if Given String can be Split into Four Distinct Strings
A string can be split into four distinct strings by identifying specific delimiters or patterns within the original string and extracting substrings based on these divisions, resulting in four separate and non-overlapping text segments. In this article, we will check if a given string can be split into four distinct strings. Table of Content Using
6 min read
Split a String into an Array of Words in JavaScript
JavaScript allows us to split the string into an array of words using string manipulation techniques, such as using regular expressions or the split method. This results in an array where each element represents a word from the original string. This can be achieved by using the following methods. Table of Content Using the split() methodUsing match
6 min read
TypeScript String split() Method
The TypeScript split() method divides a string into an array of substrings using a specified separator, a string, or a regular expression. It optionally accepts a limit to determine the maximum number of splits. This method is commonly used for string manipulation. Syntax string.split([separator][, limit])Parameter: This method accepts two paramete
3 min read
Why split the <script> tag when writing it within document.write() method in JavaScript ?
The code within the script tag is executed when the browser processes the tag. The document.write() method is used to delete all the existing content from the HTML document and inserts the new content specified within document.write() method. However, before the browser loads a page it builds the DOM tree by parsing the HTML markup. Whenever a scri
2 min read
What is split() Method in JavaScript ?
In JavaScript, the split() method is used to split a string into an array of substrings based on a specified separator. It allows you to break a string into smaller parts, which are then stored as elements in an array. Syntax:string.split(separator, limit)string: The string to be split.separator: A string or regular expression that specifies where
1 min read
Difference between split() and explode() functions for String manipulation in PHP
In this article, we will see the differences between split() and explode() functions for String manipulation in PHP. The split() and explode() functions are available in base PHP and are used to perform string manipulations as well as conversions. split() Function: The split() function in PHP is used to segregate the input string into different ele
3 min read
three90RightbarBannerImg