Javascript Explode and Implode

This is a really simple problem that many PHP developers come across when working with Javascript. Some common tasks in PHP are to explode a string or implode an array. While there are no methods in Javascript named explode or implode, the same functionality is very easy to accomplish.

First, let’s tackle PHP’s explode. To do the same thing in Javascript, you would want to use the string method split() as follows:


var myStr = "Hello World";
var myArr = myStr.split(" ");

The resulting array myArr would contain [‘Hello’, ‘World’]

Next on to PHP’s implode. To do the same thing in Javascript, you would want to use the array method join() as follows:

var myArr = [“abc”, “def “,”ghi”, “jkl”];
var myStr = myArr.join(‘,’);

The string myStr now equals ‘abc,def,ghi,jkl’

There you have it, simple, effective Javascript equivalents for PHP’s implode and explode