Table of contents
1.
Introduction
2.
Description  
3.
Syntax
4.
Parameters
5.
Return Value
6.
Example for Split String
6.1.
Example 1: Basic Split by Space
6.2.
Example 2: Split by Comma
6.3.
Example 3: Using Limit
6.4.
Example 4: Split Using Regular Expression
7.
Supported Browsers
8.
Frequently Asked Questions
8.1.
What happens if no separator is provided to the split() method?
8.2.
Can I split a string into individual characters?
8.3.
What happens if the separator is not found in the string?
9.
Conclusion
Last Updated: Jan 8, 2025
Easy

JavaScript Split String

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

The JavaScript split() method is a powerful tool used to divide a string into an array of substrings based on a specified delimiter. This method is commonly utilized for processing and manipulating text data, making it easier to work with strings in JavaScript applications. Whether you're extracting individual words, separating data values, or parsing complex inputs, the split() method provides flexibility and precision.

In this article, you will learn about the syntax of the split() method, its parameters, and practical examples to effectively split and manipulate strings in JavaScript.

Description  

The `split()` method is a built-in function in JavaScript that allows you to divide a string into an array of substrings. It takes a separator as an argument, which determines where the string should be split. The result is an array containing the separated parts of the original string.  

For example, if you have a string like `"apple,banana,orange"` & you use a comma (`,`) as the separator, the `split()` method will break the string into an array like this: `["apple", "banana", "orange"]`.  

Syntax

The syntax of the split() method is simple and easy to follow. It is used as follows:

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

 

  • string: This is the original string that you want to split.
     
  • separator (optional): This defines the pattern at which the string will be split. It can be a string or a regular expression.
     
  • limit (optional): This sets the maximum number of splits to be made. If not provided, it will split the string at every occurrence of the separator.
     

Let’s look at an example:  

let fruits = "apple,banana,orange";
let result = fruits.split(",");
console.log(result); 
You can also try this code with Online Javascript Compiler
Run Code

 

Output: 

["apple", "banana", "orange"]


In this example, the string `fruits` is split at every comma, resulting in an array of three elements.  

Note: The `split()` method is versatile & can handle various separators, like spaces, hyphens, or even regular expressions. It’s a very important tool for tasks like parsing CSV data, breaking down sentences into words, or extracting specific parts of a string.  

Parameters

Let's go over the parameters of the split() method in detail:

  1. Separator:
    • This is the pattern that the string is split by. It can be:
      • string: For example, a comma, space, or any other character.
         
      • regular expression: This allows more complex patterns, such as splitting by multiple characters or specific positions.
         
    • If no separator is provided, the string will be split into an array of individual characters.
  2. Limit:
    • This is an optional parameter. It sets a maximum number of splits to be made.
       
    • The result will contain a number of elements up to this limit, even if more would be generated by the separator.

Return Value

The split() method returns a new array of substrings. Each element of the array represents a part of the original string, split based on the specified separator.

  • If the separator is not found in the string, the result will be an array containing the entire string as the only element.
     
  • If the separator is an empty string (""), each character of the string will be returned as a separate element in the array.
     
  • If the limit is specified, the returned array will contain at most the number of elements defined by the limit.

Example for Split String

Example 1: Basic Split by Space

Let’s split a string by a space:

let str = "Hello World JavaScript";
let result = str.split(" ");
console.log(result);
You can also try this code with Online Javascript Compiler
Run Code


Output:

["Hello", "World", "JavaScript"]


Explanation: Here, we used a space as the separator. The string str was split into three substrings: "Hello", "World", and "JavaScript". The split() method returned an array of these three parts.

Example 2: Split by Comma

Now, let’s split a string by a comma:

let str = "apple,banana,orange";
let result = str.split(",");
console.log(result);
You can also try this code with Online Javascript Compiler
Run Code


Output:

["apple", "banana", "orange"]


Explanation: In this example, the string is split wherever a comma appears. The result is an array with three elements: "apple", "banana", and "orange".

Example 3: Using Limit

You can also set a limit on the number of splits. Let’s see how:

let str = "one two three four five";
let result = str.split(" ", 3);
console.log(result);
You can also try this code with Online Javascript Compiler
Run Code


Output:

["one", "two", "three"]


Explanation: Here, we’ve limited the split to three parts. Even though there are more words in the string, the result contains only the first three elements because we set the limit parameter to 3.

Example 4: Split Using Regular Expression

Regular expressions allow us to split strings in more complex ways. Let’s split a string by multiple spaces:

let str = "apple   banana  orange";
let result = str.split(/\s+/);
console.log(result);
You can also try this code with Online Javascript Compiler
Run Code


Output:

["apple", "banana", "orange"]


Explanation: In this example, we used a regular expression (/\s+/) to split the string at one or more spaces. This is useful when you don’t know how many spaces are between words.

Supported Browsers

The split() method is widely supported across all modern web browsers, including:

  • Google Chrome
     
  • Mozilla Firefox
     
  • Microsoft Edge
     
  • Safari
     
  • Opera
     

It is also supported in older versions of Internet Explorer (starting from version 9). If you’re working on a project that needs to support older browsers, ensure that you test your implementation accordingly.

Frequently Asked Questions

What happens if no separator is provided to the split() method?

If no separator is given, the entire string will be returned as a single element in an array. For example, "Hello".split() will return ["Hello"].

Can I split a string into individual characters?

Yes, you can. If you pass an empty string ("") as the separator, each character in the string will be placed into an individual element of the array. For example, "Hello".split("") will return ["H", "e", "l", "l", "o"].

What happens if the separator is not found in the string?

If the separator is not found, the split() method returns the entire string as the only element of the array. For example, "Hello".split(",") will return ["Hello"].

Conclusion

In this article, we’ve discussed the JavaScript split() method in detail. We learned how to use it to break down strings into arrays of substrings based on a separator, and how to use the optional limit parameter to control the number of splits. We also covered how the method works with different separators, including spaces, commas, and regular expressions. 

You can also check out our other blogs on Code360.

Live masterclass