Anjan Dutta

Explode or split a string in javascript

Explode or split a string in javascript

We can use the split() function to explode or split a string in javascript.

The split function takes one parameter as input. The parameter is the separator. Based on this separator we can explode or split the string into multiple parts.

The split function returns an array that contains all the subparts as ordered array items.

var str = 'Hello World'
var subparts = str.split(" ");
// >  ["Hello", "World"]

In the above example, we are splitting the string based on the single space " " separator.

Here is another example.

var str = '18-07-2021'
var subparts = str.split("-");
// >  ["18", "07", "2021"]