Definition and Usage
The split() function splits a given string based on a specified delimiter or a regular expression and returns a new array.
This function is useful for processing strings that are split according to specific rules, such as parsing URLs.
Features
- The string is split, and each part becomes an element in the resulting array.
- Because this function returns a new array, the original string is not modified.
Basic Example
/* Splitting a string using a specific delimiter */
const sentence = "JavaScript is a client-side scripting language.";
const wordsArray = sentence.split(" "); // Split the string into an array based on spaces
console.log(wordsArray);
// Output: ["JavaScript", "is", "a", "client-side", "scripting", "language."]
/* Splitting a string using a regular expression */
const str = "This is an example.";
const words = str.split(/\s+/); // Split the string into an array using a whitespace regular expression (/\s+/)
console.log(words[0]); // Output: "This"
console.log(words[1]); // Output: "is"
console.log(words[2]); // Output: "an"
console.log(words[3]); // Output: "example."
/* Parsing a URL */
const url = "https://www.example.com/path/to/file.html?param=value";
const parts = url.split("?"); // Split the URL into an array using "?" as the delimiter
console.log(parts[0]); // Output: "https://www.example.com/path/to/file.html"
console.log(parts[1]); // Output: "param=value"
Syntax
str.split()
str.split(separator)
str.split(separator, limit)
str is the string to which the split() function is applied.
Parameters
separator |
Optional.
The delimiter used to split the string. This can be specified as a string or a regular expression. |
|---|---|
limit |
Optional.
An integer that specifies the maximum length of the array created when the string is split. If omitted, the string is split using all occurrences of the separator. Any remaining parts of the string beyond the specified limit are not included in the returned array. |
Return Value
The function returns an array containing the parts of the string that were split based on the specified separator.
Example with Parameters and Return Values
When no parameters are provided
If no separator is specified, the returned array contains the original string as its only element.
const sentence = "Hello. Welcome!";
const wordsArray = sentence.split();
console.log(wordsArray);
// Output: ["Hello. Welcome!"]
When an empty string ("") is specified as the separator
If the separator is an empty string (""), the returned array contains each character of the original string as an element.
const sentence = "Hello. Welcome!";
const wordsArray = sentence.split("");
console.log(wordsArray);
// Output: ["H", "e", "l", "l", "o", ".", " ", "W", "e", "l", "c", "o", "m", "e", "!"]
When the separator appears at the beginning or end of the original string
If the specified separator appears at the beginning or end of the original string, the returned array also starts or ends with an empty string ("").
const sentence = ",Hello. Welcome!";
// Use "," as the separator at the beginning of the string
const wordsArray = sentence.split(",");
// The returned array starts with an empty string ("")
console.log(wordsArray);
// Output: ["", "Hello. Welcome!"]
const sentence = "Hello. Welcome!,";
// Use "," as the separator at the end of the string
const wordsArray = sentence.split(",");
// The returned array ends with an empty string ("")
console.log(wordsArray);
// Output: ["Hello. Welcome!", ""]
const sentence = ",Hello. Welcome!,";
// Use "," as the separator at both the beginning and end of the string
const wordsArray = sentence.split(",");
// The returned array starts and ends with an empty string ("")
console.log(wordsArray);
// Output: ["", "Hello. Welcome!", ""]
When the separator is the same as the original string
If the separator is identical to the original string, the returned array contains two empty strings ("").
const sentence = "H";
const wordsArray = sentence.split("H");
console.log(wordsArray);
// Output: ["", ""]
When both the separator and the original string are empty strings ("")
If both the separator and the original string are empty strings (""), an empty array is returned.
const sentence = "";
const wordsArray = sentence.split("");
console.log(wordsArray);
// Output: []
Practical Examples
The split() function can be used effectively in a wide range of situations. This section covers several practical examples.
Splitting a String into Words
const str = "This is an example.";
const words = str.split(" ");
console.log(words);
// Output: ["This", "is", "an", "example."]
Parsing a String
const time = "02:30:45";
const timeParts = time.split(":");
const hours = timeParts[0];
console.log("Hours:", hours);
// Output: "Hours: 02"
Extracting a File Name from a File Path
const filePath = "/path/to/file.txt";
/* Split by "/" and return the last element of the array using the pop() function */
const fileName = filePath.split("/").pop();
console.log(fileName);
// Output: "file.txt"
Additional Explanation
The pop() function removes the last element from an array and returns it.
Extracting a Domain from an Email Address
const email = "user@example.com";
const parts = email.split("@");
const domain = parts[1];
console.log(domain);
// Output: "example.com"
Compatibility
| Method |
Desktop Chrome
|
DesktopDesktop Edge
|
Desktop Firefox
|
Safari
|
Node.js
|
|---|---|---|---|---|---|
split()
|
1 | 12 | 1 | 1 | 0.10 |
Specifications
| Specification | |
|---|---|
split()
|
ECMAScript® 2026 Language Specification #sec-string.prototype.split |