Open In App

JavaScript String split() Method

The split() method in JavaScript is used to split a string into an array of substrings based on a specified separator.

String split() Method 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. 



String split() Method Example: 

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




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

String split() Method Example:

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




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

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

Supported Browsers:


Article Tags :