by Dinesh Thakur Category: Javascript String

JavaScript String split() Method: A delimiter is a sequence of one or more characters used to specify the boundary between separate, independent regions in plain text or other data streams. An example of a delimiter is the comma character, which acts as a field delimiter in a sequence of comma-separated values; each of the substrings is an element in the list.

For instance, the following string has three elements separated by commas:

"First element, Second element, Third element"

The string object provides the JavaScript Split Method, which you can use to split a string into elements at a specified delimiter. These elements are then placed in an array, and that array is returned by the method.

For instance, consider the following example of JavaScript Split Method:

var thisVar = "First element,Second element,Third element";

var anotherVar = thisVar.split(",");

anotherVar is now an array containing three elements.

The following task illustrates JavaScript Split Method this by splitting a string containing a list into its component elements and then outputting those elements from the resulting array:

1. Open a new HTML document in your preferred HTML or text editor.

2. Create the body of the document with opening and closing body tags:


3. Insert a script block in the body of the document:

<script language="JavaScript">



4. Create a variable named myVariable and assign a comma separated text string to it:

var myVariable = "a,b,c,d";

5. Use the JavaScript Split Method to split the string at the commas and assign the resulting array to the variable stringArray:

var stringArray = myVariable.split(",");

6. Use the document.write method to output the elements of the array so that the final script looks like


<script language="JavaScript">
<!-- var
   myVariable = "a,b,c,d";
    var stringArray = myVariable.split(",");
   document.write(stringArray[3]); // -->

7. Save the file and close it.

8. Open the file in a browser, and you should see the text “abcd”,

About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.

Search Content