Skip to content

JS Array Prototype Join

Quincy Larson edited this page Aug 20, 2016 · 1 revision

JavaScript Array.prototype.join()

The JavaScript array method .join() will combine all elements of an array into a single string.

Syntax

var array = ["Lorem", "Ipsum", "Dolor", "Sit"];
var str = array.join([separator]);

Parameters

separator

Optional. Specifies the string to use to separate each element of the original array. If the separator is not a string, it will be converted to a string. If separator parameter is not provided, array elements are separated with a comma by default. If separator is an empty string "", all array elements are joined without a separator character between them.

Description

.join() joins all elements of an array into a single string. If any of the array elements are undefined or null, that element is converted to the empty string "".

Examples

Using .join() four different ways

var array = ["Lorem", "Ipsum", "Dolor" ,"Sit"];

var join1 = array.join();           /* assigns "Lorem,Ipsum,Dolor,Sit" to join1 variable
                                       (because no separator was provided .join()
                                       defaulted to using a comma) */
var join2 = array.join(", ");       // assigns "Lorem, Ipsum, Dolor, Sit" to join2 variable
var join3 = array.join(" + ");      // assigns "Lorem + Ipsum + Dolor + Sit" to join3 variable
var join4 = array.join("");         // assigns "LoremIpsumDolorSit" to join4 variable

Source MDN

Clone this wiki locally