JavaScript Tutorial

JavaScript Array to String: toString(), join() and More

TGTeam GyaniPanditNovember 18, 20264 min read

Hello friends! Converting an array to a string is something you do all the time in JavaScript: showing a list of names on a page, building a sentence, saving data or sending it to a server. There are several ways to do it, and each gives a slightly different result. In this lesson, let’s compare them one by one, with real outputs, so you know exactly which one to use when.

1. toString(): Comma-Separated, No Spaces

const fruits = ["apple", "mango", "banana"];
console.log(fruits.toString());

Output:

apple,mango,banana

toString() always uses a comma with no space. You can’t change the separator.

2. join(): Choose Your Own Separator

console.log(fruits.join());        // default is a comma
console.log(fruits.join(", "));
console.log(fruits.join(" | "));
console.log(fruits.join(""));      // no separator
console.log(fruits.join("n"));    // one item per line

Output:

apple,mango,banana
apple, mango, banana
apple | mango | banana
applemangobanana
apple
mango
banana

join() is the most useful method here, because you control exactly how the items are separated. Read more: JavaScript array join()

3. String(), Template Literals and +

JavaScript converts an array to a string automatically when you use it as text. Internally it calls toString():

console.log(String(fruits));
console.log(`${fruits}`);
console.log("Fruits: " + fruits);

Output:

apple,mango,banana
apple,mango,banana
Fruits: apple,mango,banana

So far our arrays were simple. But what happens with trickier values? Let’s check.

Nested Arrays, null and undefined

console.log([1, [2, 3], [4, [5]]].toString());
console.log([1, null, undefined, 4].join("-"));
console.log([].join(", ") === "");

Output:

1,2,3,4,5
1---4
true

Nested arrays are flattened into one list, null and undefined become empty strings, and an empty array gives an empty string.

Now here is a situation that confuses many beginners: an array of objects.

Arrays of Objects: Use map() First

const users = [{ name: "Riya" }, { name: "Aman" }];
console.log(users.toString());
console.log(users.map(u => u.name).join(", "));

Output:

[object Object],[object Object]
Riya, Aman

Objects turn into the unhelpful [object Object]. Pick the field you want with map(), then join().

All the methods above are good for showing data to people. But when you need to save data or send it to a server, there is a better tool.

JSON.stringify(): When You Need to Store or Send Data

console.log(JSON.stringify(fruits));
console.log(JSON.stringify(users));

Output:

["apple","mango","banana"]
[{"name":"Riya"},{"name":"Aman"}]

Use JSON.stringify() for localStorage or APIs, because you can turn it back into the same array with JSON.parse(). A comma-joined string cannot reliably be turned back if an item itself contains a comma.

Practical Examples

Readable Sentence from a List

const names = ["Riya", "Aman", "Kabir"];
const sentence = names.slice(0, -1).join(", ") + " and " + names.at(-1);
console.log(sentence);

const lf = new Intl.ListFormat("en", { style: "long", type: "conjunction" });
console.log(lf.format(names));

Output:

Riya, Aman and Kabir
Riya, Aman, and Kabir

Numbers in Indian Format

console.log([1234567.891, 99].map(n => n.toLocaleString("en-IN")).join(" / "));

Output:

12,34,567.891 / 99

Back from String to Array

console.log("apple,mango,banana".split(","));

Output:

[ 'apple', 'mango', 'banana' ]

Which Method Should You Use?

Friends, with so many options it is easy to get confused. Here is a quick summary:

Need Use
Quick comma list toString() or String(arr)
Custom separator (“, “, ” | “, new lines) join(separator)
Array of objects map() then join()
Save or send data JSON.stringify()
Natural sentence (“A, B and C”) Intl.ListFormat

So friends, remember the key points: both toString() and join() return a string, but toString() always separates items with a comma, while join() lets you choose the separator. Neither of them changes the original array. And when you need to turn the string back into an array, use split() for a plain list, or JSON.parse() if you saved it with JSON.stringify(). Try each method in your browser console and you will remember the differences easily.

Related: Arrays in JavaScript · Array methods · Array length

Turn reading into skillsCertificate courses in AI, Data Science and programming.
Explore Courses