Code Logo

Join Array with join()

Published at25 Jul 2026
JavaScript Collections Easy 0 views
Like0

Write a JavaScript function that takes an array of strings and a separator, and returns a single string where each element is joined by the separator using Array.join().

Array.prototype.join() is a JavaScript method that creates and returns a new string by concatenating all elements in an array, separated by a specified separator string. If the separator is omitted, elements are separated with a comma. If an element is undefined or null, it is converted to an empty string.

The join() method is the inverse of split(). Together they form a powerful pattern for string manipulation: split a string into an array, manipulate the array, then join it back. This pattern is idiomatic JavaScript and is widely used for formatting output, CSV generation, and URL construction.

Time complexity is O(n) where n is the total length of the resulting string. Space complexity is O(n) for the result string. The method does not modify the original array.

Edge cases include empty array (returns empty string), single-element array (returns the element without separator), undefined/null elements (converted to empty string), and using an empty string as separator (concatenates without any gap).

Example Input & Output

Example 1
Input
["a","b","c"],"-"
Output
"a-b-c"
Explanation

Join with hyphen

Example 2
Input
["hello","world"]," "
Output
"hello world"
Explanation

Join with space

Example 3
Input
["x","y","z"],""
Output
"xyz"
Explanation

Join with empty string

Example 4
Input
["single"],","
Output
"single"
Explanation

Single element, no separator added

Example 5
Input
[],","
Output
""
Explanation

Empty array returns empty string

Algorithm Flow

Recommendation Algorithm Flow for Join Array with join()
Recommendation Algorithm Flow for Join Array with join()

Solution Approach

function solution(arr, sep) {
  return arr.join(sep);
}

Best Answers

javascript - Approach 1
function solution(arr, sep) {
  return arr.join(sep);
}