Javascript Array Join

Javascript Array Join

The join method is pretty interesting to be used. When we are using the toString method, we are getting the string, where all the array elements are separated by commas. But here, with the join method, we are again getting a string, but this time, we can specify the separator.

This simply means that with the join method, we can have such a string, where all the array elements are separated by an ‘@’ symbol, or some other separator, which we can choose to give.

Javascript Array Join

Here, we have to provide the separator to the method. Have a look at the below program to understand the use of the join method. Here, underscore is used as the separator.

You can see in the above program, we have an array, and we are using the join method as well, with an underscore as a separator.

Let’s have a look at the output now →

As you can see, we could join the array of elements according to the given separator. Here, the given separator was underscored, which can be seen between those array elements. You can try giving some other separator. We can use this method as and when required.