The “Join” Method in JavaScript

Pollet Obuya
1 min readApr 23, 2021

The “join” method in JavaScript is used on Arrays irrespective of the datatype of each element of the array. Just like the name of the method suggests, the array’s elements will be joined together. The result of the join is a string with a comma(,) as the separator for the elements by default. For example;

var electronics = ["television", "computer", "printer"]

When Array.join() is called on our array as shown below;

electronics.join();

we get;

"television,computer,printer"

Another way of using the join method is by giving it a separator. In this case, the elements will be joined using the separator given instead of a comma. This can be done like this;

electronics.join(" and ");

In this case, the elements of the array will be joined and separated with the given separator (“ and ”). The string returned will look like this;

"television and computer and printer"

The Array.join() method can also be used on an array with elements with different data types. For example;

var items = ["computer", 100, ["sun"], stars];items.join();

will return a string like this;

"computer,100,sun,stars"

if one of the elements is a collection, only the elements of the collection will be used to create the joined string as shown above.

It is important to note that there are exceptions, if one of the elements of the array is an object, Array.join() will not return the expected result. For example for an array like this;

var items = ["television", ["computer"], {"telephone": 1}, "printer"]items.join()

the string returned will be;

"television,computer,[object Object],printer"

--

--