The Wayback Machine - http://web.archive.org/web/20230316055910/https://www.geeksforgeeks.org/javascript-string-split-method/amp/

JavaScript String split() Method

JavaScript String split() Method is used to split the given string into an array of strings by separating it into substrings using a specified separator provided in the argument.

Syntax: 

str.split(separator, limit)

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

Below is an example of the String split() Method. 

Example 1: 




<script>
    // JavaScript Program to illustrate split() function 
      
    function func() { 
        //Original string 
        var str = 'Geeks for Geeks'
        var array = str.split("for"); 
       console.log(array); 
    } 
    func(); 
</script>

Output: 

Geeks , Geeks

Examples of the above function are provided below:



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




<script>
// JavaScript Program to illustrate split() function
  
function func() {
    //Original string
    var str = 'It iS a 5r&e@@t Day.'
    var array = str.split(" ");
    console.log(array);  
}
func();
</script>

Output: 

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

Example 3: In this example, the function split() creates an array of strings by splitting str wherever ” ” occurs. The second argument 2 limits the number of such splits to only 2.




<script>
// JavaScript Program to illustrate split() function
  
function func() {
  
    // Original string
    var str = 'It iS a 5r&e@@t Day.'
  
    // Splitting up to 2 terms
    var array = str.split(" ",2);
    console.log(array); 
}
func();
</script>

Output:

[It,iS]

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

Supported Browsers:

JavaScript is best known for web page development but it is also used in a variety of non-browser environments. You can learn JavaScript from the ground up by following this JavaScript Tutorial and JavaScript Examples.

We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript.


Article Tags :