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);
Parameters:
- 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.
Example 1: Here is the basic example of the split() method.
JavaScript
function func() {
let str = 'Geeks for Geeks'
let array = str.split("for");
console.log(array);
}
func();
|
Output
[ 'Geeks ', ' Geeks' ]
Example 2: In this example, the function split() creates an array of strings by splitting str wherever ” ” occurs.
JavaScript
function func() {
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.' ]
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.
JavaScript
function func() {
let str = 'It iS a 5r&e@@t Day.'
let array = str.split(" ", 2);
console.log(array);
}
func();
|
Example 4: In this example using split(”) to convert each character of the string into an array.
Javascript
let str = 'JavaScript';
let result = str.split('');
console.log(result);
|
Output
[
'J', 'a', 'v', 'a',
'S', 'c', 'r', 'i',
'p', 't'
]
We have a complete list of Javascript string methods, to check those please go through this Javascript String Complete reference article.
Supported Browsers:
- 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.
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.
Learn to code easily with our course
Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated :
07 Dec, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...