list.join

Category:Variables

[list].join

Category: Variables

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

Since our collections of data may change while the app is running, we might need to know how many items are currently in a list (array). length will tell us how big the array is. Even if some of the elements in the array are empty strings or zero, they are counted by length.

In addition to the array variable name, items in your array are numbered with an index. The first element in an array is has index 0 and the second had index 1 and so on. As a result the last index is always one less than the length of the array.

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