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.

Recent JavaScript Tutorials

Javascript FSO ShortPath property

Javascript FileSystemObject (FSO) provides a ShortPath property that can be accessed using the folder object or file object returned by the GetFolder or GetFile method.

Javascript FSO Path Property

Javascript FileSystemObject (FSO) provides the Path property that can be accessed through the file object or folder object returned by the GetFile or GelFolder method

Javascript FSO Type property

Javascript FileSystemObject (FSO) provides a Type property that can be accessed through file object or folder object returned by the GetFile or GetFolder method

Javascript FSO ShortName property

Javascript FileSystemObject (FSO) provides a ShortName property that can be accessed using the folder object or file object returned by the GetFolder or GetFile method.

Javascript FSO Name Property

Javascript FileSystemObject (FSO) provides a Name property that can be accessed using file object or folder object.