list.join

Category:Variables

[list].join

Category: Variables

Returns the array as a string with elements joined by an optional separator

Sometimes we want to put all of the items in our lists together. join allows us to do this and to choose what we want in between each of the items. The separator we choose will be placed between every single item in the resulting string.

Examples


Example: Simple Join Join a list of strings with the word "and"

var colors = ["red","orange","yellow","green","blue"];
console.log("My favorite colors are " + colors.join(" and "));

Example: Multiline-String Join a string so that each element prints on another line.

var colors = ["red","orange","yellow","green","blue"];
console.log("My favorite colors are:\n" + colors.join(" \n "));

Syntax

[list].join(separator)

Parameters

Name Type Required? Description
list variable name Yes The variable name of the list (array) you want to join
separator string No The string to use to join the list (array). If none is given a comma will be used

Returns

The list (array) as a single string, connected by the given separator.

Tips

  • If you use the newline character "\n" as your separator you can create a list in which each element prints on a different line.

Found a bug in the documentation? Let us know at documentation@code.org

Found a bug in the documentation? Let us know at documentation@code.org