How to Join an Array of Strings in TypeScript with join()

A support team asked me to join the order tags in their TypeScript dashboard into one tidy line instead of showing a raw array of strings. Their first try, a for loop with +=, left a stray comma after the last tag.

The fix is a single built-in method, and it never adds that extra comma.

Quick answer: call join() on the array and pass the separator you want. It goes only between items, and you get back one plain string:

const tags: string[] = ["rush", "gift", "fragile"];
const line = tags.join(", ");   // "rush, gift, fragile"

Every snippet here was compiled with --strict on TypeScript 7.0.2 and Node.js v22.22.2 in VS Code, so each output is what the terminal printed.

In this tutorial, I’ll join an array of strings in TypeScript for jobs I actually ship: CSV rows, query strings and readable name lists, plus the empty, null and object cases that catch people out.

How Does join() Work on a TypeScript Array?

join(separator?) walks the array once and returns a new string. Leave the separator out and you get a comma with no space. Here’s one tag list joined five ways:

const tags: string[] = ["rush", "gift", "fragile", "insured"];

console.log(tags.join());       // default: comma, no space
console.log(tags.join(", "));
console.log(tags.join(" | "));
console.log(tags.join(""));
console.log(tags.join(" and "));

const line: string = tags.join(", ");
console.log(typeof line, "-", tags.length, "tags still in the array");

Output:

rush,gift,fragile,insured
rush, gift, fragile, insured
rush | gift | fragile | insured
rushgiftfragileinsured
rush and gift and fragile and insured
string - 4 tags still in the array
VS Code running TypeScript join() on a string array with the default comma, a comma and space, a pipe, an empty string and the word and
Five separators, five strings, and the original array still holds its four tags.

TypeScript types the result as string, and the array itself doesn’t change. The opposite move, splitting text back into items, is covered in how to convert a string to an array in TypeScript.

Build a CSV Row and a Query String with join()

Same idea, real data. Each order becomes a CSV row, and a filter object becomes a URL query you can send to an API:

interface Order {
  id: number;
  customer: string;
  city: string;
  total: number;
}

const orders: Order[] = [
  { id: 1041, customer: "Jenna Morris", city: "Austin", total: 129.5 },
  { id: 1042, customer: "Marcus Hill", city: "Denver", total: 74.25 },
];

const header = ["id", "customer", "city", "total"].join(",");
const rows = orders.map((o) =>
  [o.id, o.customer, o.city, o.total.toFixed(2)].join(","),
);
console.log([header, ...rows].join("\n"));

const filters = { state: "TX", status: "shipped", q: "gift card" };
const query = Object.entries(filters)
  .map(([k, v]) => `${k}=${encodeURIComponent(v)}`)
  .join("&");
console.log(`/api/orders?${query}`);

Output:

id,customer,city,total
1041,Jenna Morris,Austin,129.50
1042,Marcus Hill,Denver,74.25
/api/orders?state=TX&status=shipped&q=gift%20card
VS Code output of TypeScript join() building a CSV header and rows and an encoded API query string
Three CSV lines and a query string with the space in gift card encoded as %20.

The order row mixes numbers and strings, and join() converts each item with String(). I format money with toFixed(2) first so $129.50 doesn’t print as 129.5. Turning objects into rows like this pairs with mapping an array of objects in TypeScript.

Note: real CSV fields that contain commas need quotes. Excel splits an unquoted Austin, TX into two columns. For query strings, URLSearchParams does the encoding for you.

Readable Name Lists: join() vs Intl.ListFormat in TypeScript

For a sentence like “Jenna, Marcus, and Priya”, a single separator isn’t enough. You can split off the last two names, or let Intl.ListFormat handle it:

const team: string[] = ["Jenna", "Marcus", "Priya"];

const lastTwo = team.slice(-2).join(" and ");
const manual = [...team.slice(0, -2), lastTwo].join(", ");
console.log("manual:    ", manual);

const and = new Intl.ListFormat("en-US", {
  style: "long",
  type: "conjunction",
});
console.log("ListFormat:", and.format(team));
console.log("two names: ", and.format(["Jenna", "Marcus"]));

Output:

manual:     Jenna, Marcus and Priya
ListFormat: Jenna, Marcus, and Priya
two names:  Jenna and Marcus

My manual version drops the Oxford comma, while Intl.ListFormat adds it for en-US and you get the two-name case right for free.

It needs es2021 or later in your TypeScript lib setting. For gluing a few known values instead of a whole array, see my TypeScript string concatenation tutorial.

Empty Arrays, Nulls and Objects in join()

Most surprises you’ll hit come from what’s inside the array, not from the method. Here’s every odd case I’ve run into on client code:

const empty: string[] = [];
console.log(`empty:    "${empty.join(", ")}"`);
console.log(`one item: "${["Austin"].join(", ")}"`);

type Note = string | null | undefined;
const notes: Note[] = ["call back", null, "VIP", undefined];
console.log(`gaps:     "${notes.join(", ")}"`);
const clean = notes.filter((n): n is string => Boolean(n));
console.log(`filtered: "${clean.join(", ")}"`);

const nested = [["TX", "CA"], ["NY"]];
console.log(`nested:   "${nested.join(" | ")}"`);

const people = [{ name: "Jenna" }, { name: "Marcus" }];
console.log(`objects:  "${people.join(", ")}"`);
console.log(`mapped:   "${people.map((p) => p.name).join(", ")}"`);

Output:

empty:    ""
one item: "Austin"
gaps:     "call back, , VIP, "
filtered: "call back, VIP"
nested:   "TX,CA | NY"
objects:  "[object Object], [object Object]"
mapped:   "Jenna, Marcus"
VS Code output of TypeScript join() on an empty array, one item, null and undefined values, a nested array and an array of objects
An empty string, no separator for one item, blank gaps for null and undefined, and [object Object] for plain objects.

null and undefined turn into empty strings, which leaves doubled separators. Filter them out first with a type guard, as in filtering arrays in TypeScript. Objects need a map() to the field you want.

Nested arrays get flattened with commas, whatever separator you pass. If the empty case matters, my post on an empty string array in TypeScript shows how to check for it.

join() Errors You’ll See in TypeScript

Strict TypeScript catches the two mistakes I see most: a number as the separator, and treating the result as something other than a string:

const zips: string[] = ["78701", "80202", "98101"];

const line = zips.join(0);
const count: number = zips.join("+");

Terminal:

join_errors.ts(3,24): error TS2345: Argument of type 'number' is not assignable to parameter of type 'string'.
join_errors.ts(4,7): error TS2322: Type 'string' is not assignable to type 'number'.
VS Code showing TypeScript errors TS2345 for a number passed to join() and TS2322 for assigning the joined string to a number
tsc stops with TS2345 on the separator and TS2322 on the variable type.

Pass the separator as a string, "0" instead of 0, and type the variable as string. To add the values up instead, convert with Number() and sum them.

People also mix up join() with concat(). concat() merges arrays into a bigger array; see concatenating arrays in TypeScript.

join(), reduce() or a Loop: Picking One for String Arrays

ApproachGood forWatch for
arr.join(", ")Any separator between itemsNulls become empty gaps
Intl.ListFormatSentences with “and” or “or”Needs es2021+ lib
reduce()Custom rules per itemHarder to read
for loop with +=Rarely neededTrailing separators

I reach for join() nine times out of ten. reduce() only earns its place when each item needs its own logic, which my TypeScript array reduce tutorial walks through.

A few more TypeScript string and array posts to read next:

Quick Answers on Joining String Arrays

How do I join an array of strings in TypeScript?

Call arr.join(separator), for example tags.join(", "). It returns one string with the separator only between items.

What does join() use when I pass no separator?

A comma with no space, so ["a", "b"].join() gives "a,b". That’s also what toString() returns.

Does join() change the original array?

No. It returns a new string and leaves the array exactly as it was.

Why do I get double commas after join()?

The array has null or