October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run ScanOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk1 min

TypeScript String Arrays: How to Join Items with a Separator

Use array.join(separator) to combine TypeScript strings into one string, with the separator you choose between each item.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Call join() on the array and pass the text you want between its items. For example, words.join(" ") combines a string array into a sentence with spaces between the words.

Join a string array in TypeScript

TypeScript uses string[] or the equivalent Array<string> type for an array of strings. Call join() on the array value:

As an Amazon Associate I earn from qualifying purchases.

const words: string[] = ["TypeScript", "is", "useful"];
const sentence: string = words.join(" ");

console.log(sentence); // TypeScript is useful

join() returns a string. It does not change the original array.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose the separator for the output

The separator is the argument passed to join(). Use one that suits the format you need:

  • names.join(", ") produces Ada, Grace, Linus.
  • names.join(" and ") produces Ada and Grace and Linus.
  • names.join("") produces AdaGraceLinus.
const names: string[] = ["Ada", "Grace", "Linus"];

const commaSeparated = names.join(", ");
const conjunction = names.join(" and ");
const concatenated = names.join("");

The separator appears between neighboring items, not before the first item or after the last.

Narrow a string-or-array value before joining

If a value can be either a string or a string array, TypeScript will not let you call an array-only method on the union without first narrowing its type. Use Array.isArray() to distinguish the array case:

function display(value: string | string[]): string {
  if (Array.isArray(value)) {
    return value.join(" and ");
  }
  return value;
}

Inside the array branch, value is treated as an array, so join() is available. The other branch returns the string unchanged.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Type the array as strings

Both string[] and Array<string> describe an array whose items are strings:

const first: string[] = ["one", "two"];
const second: Array<string> = ["one", "two"];

Either spelling works with join(); choose the style that matches the surrounding code.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.