JavaScript split Method – Create Array From String

***If this post helped you, please support this blog by clicking one of the links on this page. Thanks :) ***

The JavaScript split method is used to create an array from a string.

var strTest = '1,2,3,4';
var aryTest = strTest.split();
document.write(typeof aryTest);

The preceding example demonstrates that when the split method is applied on a string an array object is created. The split method takes an optional argument: The delimiter on which to base the split. Comma is the default.

var strTest = '1|2,3|4';
var aryTest = strTest.split('|');
document.write(aryTest.length);

In this example the split occurs based on a pipe (|) delimiter, so there will only be three elements in this array:

aryTest[0] = 1
aryTest[1] = 2,3
aryTest[2] = 4

2 Responses to “JavaScript split Method – Create Array From String”

  1. Fernando Koch Says:

    Thanks for example!

  2. Shashi Says:

    Thanks for such a neat and nice working example.
    Just want exactly like this one.

Leave a Reply