JavaScript How to Convert Array to String Easily
You can convert an array to a string in JavaScript by using
array.join(separator) to join elements with a separator or array.toString() which joins elements with commas by default.Examples
Input[1, 2, 3]
Output"1,2,3"
Input["apple", "banana", "cherry"]
Output"apple,banana,cherry"
Input["a", "b", "c"].join("-")
Output"a-b-c"
How to Think About It
To convert an array to a string, think about how you want the elements to appear joined together. You can use
join() to specify a separator like a comma, space, or dash, or use toString() which automatically joins elements with commas.Algorithm
1
Get the input array.2
Choose a method to convert it to string: either <code>join()</code> or <code>toString()</code>.3
If using <code>join()</code>, decide on a separator string.4
Call the chosen method on the array.5
Return the resulting string.Code
javascript
const fruits = ["apple", "banana", "cherry"]; const result1 = fruits.toString(); const result2 = fruits.join(" - "); console.log(result1); console.log(result2);
Output
apple,banana,cherry
apple - banana - cherry
Dry Run
Let's trace converting ["apple", "banana", "cherry"] to string using join(" - ")
1
Start with array
["apple", "banana", "cherry"]
2
Call join with separator " - "
"apple" + " - " + "banana" + " - " + "cherry"
3
Return joined string
"apple - banana - cherry"
| Index | Element | String after join step |
|---|---|---|
| 0 | apple | apple |
| 1 | banana | apple - banana |
| 2 | cherry | apple - banana - cherry |
Why This Works
Step 1: Using toString()
toString() converts the array elements into a comma-separated string automatically.
Step 2: Using join(separator)
join() lets you specify how to connect elements, like spaces, dashes, or no separator.
Alternative Approaches
Using JSON.stringify()
javascript
const arr = [1, 2, 3]; const str = JSON.stringify(arr); console.log(str);
This converts the array to a JSON string including brackets and quotes, useful for data transfer but not for simple joining.
Using a loop to build string
javascript
const arr = ['a', 'b', 'c']; let str = ''; for (const item of arr) { str += item; } console.log(str);
Manually concatenates elements without separators; less efficient and more code.
Complexity: O(n) time, O(n) space
Time Complexity
Converting an array to string requires visiting each element once, so it takes O(n) time where n is the number of elements.
Space Complexity
The output string stores all elements joined, so it uses O(n) space proportional to the input size.
Which Approach is Fastest?
toString() and join() have similar performance; join() is preferred for flexibility.
| Approach | Time | Space | Best For |
|---|---|---|---|
| toString() | O(n) | O(n) | Quick comma-separated string |
| join(separator) | O(n) | O(n) | Custom separator strings |
| JSON.stringify() | O(n) | O(n) | Full JSON format with brackets |
| Manual loop concat | O(n) | O(n) | Custom complex formatting but verbose |
Use
join() when you want control over the separator between array elements.Forgetting that
toString() always uses commas and doesn't allow custom separators.