What does join() do?
The join() method in JavaScript is used to combine all array elements into one string, separating them with a specified separator.
Example:
javascript
const fruits = ["apple", "banana", "cherry"];
const result = fruits.join(", ");
console.log(result); // "apple, banana, cherry"- Each array element is turned into a string.
- The separator
", "is inserted between elements. - If no separator is specified, a comma is used by default.
Syntax:
javascript
array.join(separator);| Parameter | Description |
|---|---|
separator | (optional) a string to insert between elements. If not specified, "," is used. |
A few examples:
Without a separator:
javascript
[1, 2, 3].join("");
// "123"With a space:
javascript
["JS", "is", "awesome"].join(" ");
// "JS is awesome"With another character:
javascript
["2025", "10", "14"].join("-");
// "2025-10-14"With empty elements:
javascript
[1, , 3].join("-");
// "1--3" (the empty slot is still counted)Important:
- The method does not change the original array.
- All elements are implicitly converted to strings before being joined.
- Returns a string, not an array.
Practical use example:
javascript
const tags = ["frontend", "javascript", "interview"];
const hashtagString = "#" + tags.join(" #");
console.log(hashtagString);
// "#frontend #javascript #interview"Summary:
| What it does | Converts an array into a string |
|---|---|
| Default separator | "," |
| Returns | A string |
| Changes the array | No |
| Example | ["a", "b"].join("-") -> "a-b" |
In summary:
join()is a way to assemble an array into a string, inserting the needed separator between elements.
Short Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.