Array to String in JavaScript: Practical Guide
Learn practical ways to convert arrays to strings in JavaScript using join, toString, JSON.stringify, and more. Includes examples, edge cases, and performance tips for developers.

Converting an array to a string in JavaScript is a common task for display or serialization. The most typical methods are join and toString, which control separators and formatting. For JSON-like output, use JSON.stringify. Each approach has nuances around undefined holes and nested elements, so choose based on the desired string format and data shape.
Understanding the problem: array to string in javascript
In this section you’ll learn how JavaScript turns an array into a string for display, logging, or transmission. The central idea is that arrays themselves are not strings, but their elements can be serialized in a controlled way. The keyword here is that you often want to decide how to separate values and how to handle non-string elements to avoid surprises when rendering UI or sending data across the wire.
const arr = [1, 2, 3];
console.log(arr.join(", ")); // "1, 2, 3"
console.log(arr.toString()); // "1,2,3"joinlets you specify a separator and formats the result exactly as you intendtoStringuses the default comma without spaces- Elements that aren’t strings are coerced to strings first by default
Variation: if the array has holes, join inserts empty segments rather than skipping them.
- "## Choosing the right method: join vs toString vs JSON.stringify"
When you need full control over the separator, prefer join. For a quick comma-delimited string, toString is concise. If you want a JSON-like representation suitable for network transport, use JSON.stringify on the array. Each method has a different output shape and use case, so pick accordingly.
const a = ["apple", null, true, undefined];
console.log(a.join(" | ")); // "apple | | true | "
console.log(a.toString()); // "apple,,true,"
console.log(JSON.stringify(a)); // "["apple",null,true,null]"undefinedandnullappear differently across methods; join renders empty segments for holes
Edge cases and common variations
Arrays can contain nested arrays, objects, or dates. How you stringify them depends on the result you need. toLocaleString applies locale-specific formatting to each element, while JSON.stringify preserves structure for JSON consumers. Be mindful of nested arrays, as join may flatten in unexpected ways.
const nested = [1, [2, 3], 4];
console.log(nested.join(",")); // "1,2,3,4"
console.log(JSON.stringify(nested)); // "[1,[2,3],4]"
console.log(nested.toLocaleString()); // locale-aware flattening- Use
map(String)if you want uniform string conversion before joining
Practical recipes: common patterns in real apps
You’ll often normalize data before display. Here are three practical patterns:
// Pattern A: normalize to strings, then join with a custom separator
const items = [1, true, null, {k: 1}];
const resultA = items.map(String).join(" • ");
console.log(resultA); // "1 • true • null • [object Object]"
// Pattern B: ensure JSON-safe representation for transmission
const payload = [1, "two", {three: 3}];
const resultB = JSON.stringify(payload);
console.log(resultB); // "[1,\"two\",{\"three\":3}]"
// Pattern C: simple comma-delimited for CSV-ish output
const csvLike = ["a", "b", "c"];
console.log(csvLike.join(",")); // "a,b,c"- When you want a predictable string, prefer explicit mapping and joining
Performance considerations and best practices
For large arrays, building strings with repeated concatenation is slower than using join, which is optimized in modern engines. A small micro-benchmark shows join typically outperforms += loops when constructing long strings. Always prefer join for multi-element serialization and reserve manual concatenation for tiny datasets.
const big = Array.from({length: 10000}, (_, i) => i);
// Benchmark pattern A: join
let t0 = performance.now();
const sA = big.join(",");
let t1 = performance.now();
console.log("join time:", t1 - t0);
// Benchmark pattern B: manual concat (not recommended for large arrays)
let t2 = performance.now();
let sB = "";
for (let i = 0; i < big.length; i++) {
sB += big[i] + ",";
}
let t3 = performance.now();
console.log("concat time:", t3 - t2);Steps
Estimated time: 45-60 minutes
- 1
Identify the desired string format
Decide whether you need a delimited list, a JSON-like payload, or a locale-aware string. This guides the choice of method.
Tip: Start with a concrete example of the target string. - 2
Choose the right method
Use join for custom separators, toString for default comma, or JSON.stringify for JSON output.
Tip: Favor explicit separators to avoid surprises in UI. - 3
Handle edge cases
Account for undefined, null, and nested arrays. Decide if you want to flatten or preserve structure.
Tip: Test with [1, undefined, null, [2,3]] to see behavior. - 4
Create robust helpers
Wrap common patterns in a small function to reuse logic across modules.
Tip: Example: const stringifyArray = arr => arr.map(String).join(', '); - 5
Validate output in UI or API
Log or snapshot the result to ensure it matches expectations in different locales.
Tip: Use JSON.stringify for API payloads when structure matters. - 6
Benchmark and optimize
If you process large arrays, profile with a micro-benchmark and prefer join over concatenation.
Tip: A quick test helps prevent regressions.
Prerequisites
Required
- Required
- Required
- Basic JavaScript knowledge (arrays, map, join)Required
Optional
- NPM or Yarn (optional for running code locally)Optional
Keyboard Shortcuts
| Action | Shortcut |
|---|---|
| Copy code snippet | Ctrl+C |
| Paste code snippet | Ctrl+V |
| Comment/uncomment selected lines | Ctrl+/ |
| Format document | ⇧+Alt+F |
Questions & Answers
What is the difference between join and toString for arrays?
join lets you specify a separator and formats the string exactly as you want. toString uses a default comma delimiter. For nested or complex data, join is typically the safer choice.
Join lets you set separators; toString uses a comma by default.
How do undefined or holes in an array affect the result?
Holes are treated as empty strings in join and toString. This can lead to consecutive separators or trailing separators depending on where holes occur.
Holes become empty segments when you join.
When should I use JSON.stringify on an array?
Use JSON.stringify when you need a JSON-compatible representation, such as transmitting data over a network or storing structured data.
JSON.stringify is for JSON-friendly array strings.
Can I stringify nested arrays without flattening them?
JSON.stringify preserves array structure, including nested arrays. If you only want a flat string, use join with a manual flattening step.
If you need structure, stringify; for flat strings, join after flattening.
What about locale-specific formatting for numbers?
Use toLocaleString on each element before joining to apply locale-aware formatting when needed.
Locale formatting can be applied to each element prior to joining.
What to Remember
- Use join to control string format and separators
- toString provides a quick, comma-separated result
- JSON.stringify preserves structure for data transmission
- Handle undefined and holes explicitly when formatting
- Prefer join for performance on large arrays