JavaScript split() method is used to splits a String object into an array of strings by separating the string into substrings. This method takes a pattern and divides a String
into an ordered list of substrings by searching for the pattern, puts these substrings into an array, and returns the array.
string.split(separator, limit)
split()
split(separator)
Note: This method does not change the original string and If (” “) is used as a separator, the string is split between words.
The split method in JavaScript
A simple example code divides a string into a list of substrings and returns them as an array.
<!DOCTYPE html>
<html>
<body>
<script >
const str = 'Hello World!';
const words = str.split(' ');
console.log(words);
const chars = str.split('');
console.log(chars);
const strCopy = str.split();
console.log(strCopy);
</script>
</body>
</html>
Output:
Use the limit parameter:
const myArray = text.split(" ", 3);// How,are,you
Split a string into characters and return the second character:
let text = "How are you doing today?";
const chars = text.split("");
console.log(chars[1]); // 0
Do comment if you have any doubts or suggestions on this Js string split topic.
Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version