Convert an array to a string in JavaScript (join, toString, JSON)

Convert array to string in JavaScript with join, toString, String(), toLocaleString, JSON.stringify, reduce, or loops—with example output and common pitfalls (objects, BigInt, null/undefined).

Published

Updated

Read time 4 min read

Reviewed byDeepak Prasad

Convert an array to a string in JavaScript (join, toString, JSON)

When you have a requirement to convert array to string in JavaScript—whether for logs, CSV-style text, or network payloads—you usually mean one of three things: a delimiter-separated line (javascript array to string / js array to string), a JSON snapshot (javascript convert array to string for APIs), or custom formatting (how to convert an array to string in javascript with your own layout).

Tested on: Node.js v20.18.2. After each runnable block, a short note describes what you should see in the console.


join() builds one string from every element, separated by an optional separator. If separator is omitted, the default is a comma—the same default toString() uses internally.

javascript
const colors = ["red", "green", "blue"];

console.log(colors.join());
console.log(colors.join("; "));
Output

You should see red,green,blue on the first line and red; green; blue on the second (note the space after the semicolon in the separator).


2. Array.prototype.toString() and String(array)

toString() on arrays is specified to behave like join() with no separator argument, so [1, 2, 3].toString() and [1, 2, 3].join() match. String(arr) ends up calling the same string-coercion path for ordinary arrays.

javascript
const colors = ["red", "green", "blue"];

console.log(colors.toString());
console.log(String(colors));
Output

Both lines should print red,green,blue—same comma-separated form as join() with no argument.


3. JSON.stringify() (structure-preserving text)

Use JSON.stringify when you need a JSON text form (nested arrays/objects, string quoting), not just delimiter joining.

javascript
const colors = ["red", "green", "blue"];
const colorsString = JSON.stringify(colors);

console.log(colorsString);
console.log(typeof colorsString);
Output

You should see a JSON array string with quoted elements, then string as the type of that value.


4. toLocaleString() (locale-aware numbers)

For numbers, toLocaleString can add grouping and locale rules—different from plain join.

javascript
console.log([1000, 2000].toLocaleString("en-US"));
Output

With en-US, each number is grouped with commas and the two are joined by another comma (so the line looks like 1,000,2,000).


5. map + join (typed formatting)

A common array to string js pattern is map first, then join, for example turning numbers into strings before joining.

javascript
console.log([1, 2, 3].map(String).join("-"));
Output

You should see 1-2-3.


6. Manual loop with forEach

When you need full control (prefixes, newlines, conditional separators), build a string in a loop.

javascript
const colors = ["red", "green", "blue"];
let colorsString = "";
let another = "";

colors.forEach(function (color, index) {
  colorsString += index + 1 + ". " + color + " ";
});

colors.forEach(function (color, index) {
  another += index + 1 + ". " + color + "\n";
});

console.log(colorsString);
console.log(another);
Output

The first console.log is one line of numbered colors separated by spaces; the second prints four lines (1. red through 3. blue plus a trailing blank line from the final \n).


7. reduce() with a delimiter

reduce folds the array into one value—here, a single string with a custom separator (trim the trailing dash if you add one every iteration).

javascript
const colors = ["red", "green", "blue"];
let colorsString = colors.reduce(function (accumulator, color) {
  return accumulator + color + "-";
}, "");

colorsString = colorsString.substring(0, colorsString.length - 1);
console.log(colorsString);
Output

After trimming the trailing hyphen, you should see red-green-blue.


Pitfalls (objects, holes, BigInt, JSON)

join / toString call ToString on each element. Plain objects become [object Object], while null and undefined become empty segments (extra commas).

javascript
console.log([{ a: 1 }].join());
console.log([null, undefined].join());
Output

The object becomes [object Object]; null and undefined become empty strings, so the second line is a lone comma between two empty segments.

Nested arrays stringify inner levels with their own commas, which can look “flattened” at a glance:

javascript
console.log([[1, 2], [3, 4]].join());
Output

Nested arrays flatten visually to something like 1,2,3,4 because each inner array is stringified with its own commas before the outer join.

JSON.stringify throws on BigInt elements unless you supply a custom replacer:

javascript
try {
  JSON.stringify([1n, 2n]);
} catch (e) {
  console.log(e.name);
}
Output

The catch branch runs and logs TypeError.


Summary

Goal Approach
Delimited text arr.join(sep) (or toString() / String(arr) for comma default)
Locale-aware numeric display arr.toLocaleString(...)
JSON for APIs / storage JSON.stringify(arr) (know the limits)
Custom layout forEach, reduce, or map + join

References


Frequently Asked Questions

1. How do I convert an array to a string in JavaScript?

For a flat list of values, use Array.prototype.join(separator)—omit separator for comma-separated text (same default as toString()). Use JSON.stringify(arr) when you need a portable JSON representation (arrays of objects, nesting), not just a human delimiter.

2. What is the difference between join and toString on an array?

Array toString() calls join() with no arguments, so both default to commas between elements. join lets you pick any separator (for example pipe, newline, or an empty string to concatenate digits).

3. What happens to null or undefined elements when I join an array?

join (and therefore toString) converts null and undefined to empty strings, so you may see extra commas with nothing between them unless you filter or map first.

4. Can I use JSON.stringify to convert any array to a string?

JSON.stringify works for JSON-serializable data. It rejects BigInt by default (TypeError), drops undefined array slots in some positions rules, and ignores functions and symbols in plain JSON mode—use a reviver/replacer or a different format when you hit those limits.

5. How do I convert an array of numbers to a string with dashes?

Use join with a template or map when you need formatting, for example [1,2,3].map(String).join('-') which yields 1-2-3 in modern engines.

6. How do I format numbers when turning an array into a string?

Use toLocaleString with a locale and options, for example [1000,2000].toLocaleString('en-US') for grouping—distinct from join which does not add thousands separators by itself.
Olorunfemi Akinlua

Boasting over five years of experience in JavaScript, specializing in technical content writing and UX design. With a keen focus on programming languages, he crafts compelling content and designs …