Skip to content

Latest commit

 

History

History
38 lines (27 loc) · 1.19 KB

File metadata and controls

38 lines (27 loc) · 1.19 KB
chapter 6
pageNumber 53
description The join method turns the array in a string an joins it all together without modifying the original array.

Join

The join method, makes an array turn into a string and joins it all together. It does not change the original array. Here's the syntax for using join:

array.join([separator]);

The separator argument is optional and specifies the character to be used to separate the elements in the resulting string. If omitted, the array elements are separated with a comma (,).

For example:

let array = ["one", "two", "three", "four"]; 

console.log(array.join(" ")); 

// Result: one two three four

{% hint style="warning" %} Any separator can be specified but the default one is a comma (,). {% endhint %}

In the above example, a space is used as a separator. You can also use join to convert an array-like object (such as an arguments object or a NodeList object) to a string by first converting it to an array using the Array.prototype.slice() method:

function printArguments() {
  console.log(Array.prototype.slice.call(arguments).join(', '));
}

printArguments('a', 'b', 'c'); // Result: "a, b, c"