Skip to content
Related Articles

Related Articles

JavaScript String split() Method

Improve Article
Save Article
  • Last Updated : 19 Oct, 2022
Improve Article
Save Article

JavaScript str.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)
  • 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.

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

Example: 

JavaScript




<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 1: 

var str = 'It iS a 5r&e@@t Day.'
var array = str.split(" ");
print(array);

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

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

Example 2: 

var str = 'It iS a 5r&e@@t Day.'
var array = str.split(" ",2);
print(array);

Output: 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.

[It,iS]

Codes for the above function are provided below:

Program 1:
 

JavaScript




<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.]

Program 2:

JavaScript




<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]

Supported Browser:

  • Chrome 1 and above
  • Edge 12 and above
  • Firefox 1 and above
  • Internet Explorer 4 and above
  • Opera 3 and above
  • Safari 1 and above 

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.


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!