Split Function in JavaScript is used to split any string into two or more parts by passing the separator character to the split function. Split function splits a string into an array by storing the first part at first index, second part at second index and so on.
Syntax
stringObject.split( [separator character] );
Above function returns an Array that can be passed to the array object as follows:
Example of JavaScript Split Function
var myArr = new Array();
var myString = new String();
myString = "1,2,3,4,5,6,7,8,9";
myArr = myString.split(",");
for(var i=0;i<myArr.length;i++)
{
document.write("Array Index " + i + " = " + myArr[i] + "<br />");
}
In the above example string variable is assigned some values separated with commas. Split function is used by passing the separation character “,” e.g.: myString.split(“,”)It returns an array that is passed to an Array object.
Further in the above example – JavaScript FOR loop is used to read the values of array.
Continue to next tutorial: JavaScript indexOf String Function to learn how to get the index of specified string pattern from the provided string.