How to Convert a String to an Array in TypeScript

To convert a string to an array in TypeScript, use split() with a separator, such as "a,b,c".split(","), which returns a string[]. For an array of single characters, use Array.from(str) instead of split(""):

const cities = "Austin,Denver,Seattle".split(",");   // string[]
const letters = Array.from("Texas");                  // ["T","e","x","a","s"]

Here I’m compiling in strict mode with TypeScript 7.0.2 with Node.js v22.22.2, and each output block is pasted from the console.

You’ll see split() with strings and regular expressions, character arrays that don’t break emoji, strings of numbers, and JSON text that already holds an array.

Convert a string to an array with split()

split() cuts the string at every separator and returns the pieces. TypeScript types the result as string[] automatically:

const csvRow = "Austin,Denver,Seattle,Boston";
const cities: string[] = csvRow.split(",");
console.log(cities, cities.length);

const sentence = "Ship to   Portland  today";
console.log(sentence.split(" "));                 // repeated spaces leave empty strings
console.log(sentence.trim().split(/\s+/));        // regex: any run of whitespace

console.log("".split(","));                        // not an empty array!
console.log("a,b,c,d".split(",", 2));              // limit

Output:

[ 'Austin', 'Denver', 'Seattle', 'Boston' ] 4
[ 'Ship', 'to', '', '', 'Portland', '', 'today' ]
[ 'Ship', 'to', 'Portland', 'today' ]
[ '' ]
[ 'a', 'b' ]
Command Prompt output of TypeScript split converting a comma string to an array, with a regex split and the empty string case
The city list splits into four items, the regex removes the extra spaces, and an empty string gives an array with one empty string.

Two traps show up here: double spaces create empty items, and splitting an empty string gives [""], not []. For CSV-style input with spaces after the commas, see splitting a string by comma in TypeScript.

How do I convert a string to an array of characters?

split(""), spread [...str] and Array.from(str) look the same on plain letters. Emoji and flags show the difference:

const word = "Texas";
console.log(word.split(""));
console.log([...word]);
console.log(Array.from(word));

const order = "Coffee โ˜• x2 ๐Ÿ‡บ๐Ÿ‡ธ";
console.log(order.split("").length, "items from split('')");
console.log(Array.from(order).length, "items from Array.from");
console.log(order.split("").slice(-4));
console.log(Array.from(order).slice(-2));

Output:

[ 'T', 'e', 'x', 'a', 's' ]
[ 'T', 'e', 'x', 'a', 's' ]
[ 'T', 'e', 'x', 'a', 's' ]
16 items from split('')
14 items from Array.from
[ '\ud83c', '\uddfa', '\ud83c', '\uddf8' ]
[ '๐Ÿ‡บ', '๐Ÿ‡ธ' ]
Command Prompt output of TypeScript split empty string breaking emoji into surrogate halves while Array.from keeps them whole
split(”) returns more items than Array.from for the same text, because it cuts emoji into broken halves.

split("") works on UTF-16 code units, so one emoji becomes two broken pieces. Spread and Array.from iterate by code point. A flag is still two code points, so it stays as two regional letters.

I default to Array.from() for characters. If you need the raw bytes instead, use converting a string to a byte array.

Convert a string of numbers to a number array

Values from a form or a URL arrive as one string. Split, then convert each piece with Number, and drop anything that isn’t a real number:

const prices = "19.99, 5.25, 102, abc";

const asNumbers: number[] = prices.split(",").map(Number);
console.log(asNumbers);

const clean = prices
  .split(",")
  .map((s) => Number(s.trim()))
  .filter((n) => Number.isFinite(n));
console.log(clean, "total:", clean.reduce((a, b) => a + b, 0).toFixed(2));

const zip = "02134";
console.log(Array.from(zip, Number));             // map while converting

Output:

[ 19.99, 5.25, 102, NaN ]
[ 19.99, 5.25, 102 ] total: 127.24
[ 0, 2, 1, 3, 4 ]
Command Prompt output of TypeScript splitting a price string and mapping it to numbers, with NaN filtered out
map(Number) leaves NaN for the text entry; trimming and filtering gives three clean prices and their total.

Number(" 5.25") ignores surrounding spaces, but "abc" becomes NaN. More conversion rules are in converting a string to a number in TypeScript.

Convert a JSON string to an array with JSON.parse()

If the string is already JSON, don’t split it. JSON.parse() returns any, so give it a type, or better, check it at runtime:

const body = '["Chicago","Miami","Dallas"]';

const raw = JSON.parse(body);                       // type: any
const stores: string[] = JSON.parse(body);          // annotate what you expect

function isStringArray(value: unknown): value is string[] {
  return Array.isArray(value) && value.every((v) => typeof v === "string");
}

const checked: unknown = JSON.parse('["Reno", 42]');
console.log(stores.length, typeof raw);
console.log(isStringArray(checked) ? checked : "not a string[]: " + JSON.stringify(checked));

Output:

3 object
not a string[]: ["Reno",42]
Command Prompt output of TypeScript JSON.parse turning a JSON string into an array and a type guard rejecting mixed values
The parsed store list has three entries; the type guard rejects an array that contains a number.

The annotation alone doesn’t check anything, so I use a type guard for data from outside the app. There’s more in converting JSON to an array in TypeScript.

String to array methods in TypeScript compared

InputUseResult
"a,b,c"s.split(",")["a","b","c"]
Words with uneven spacess.trim().split(/\s+/)Words only
Characters (emoji-safe)Array.from(s) or [...s]One item per code point
"1,2,3"s.split(",").map(Number)number[]
JSON textJSON.parse(s) + type guardTyped array

Going the other way, from an array back to text, is covered in converting an array to a string in TypeScript.

For more string handling in TypeScript, start with these:

Frequently asked questions

How do I convert a string to an array in TypeScript?

Use str.split(separator), for example "a,b".split(","). The result is typed as string[].

How do I split a string into characters?

Use Array.from(str) or [...str]. split("") breaks emoji into two pieces.

Why does “”.split(“,”) return [“”]?

split() always returns at least one piece. Check for an empty string first, or filter out empty items.

How do I convert “1,2,3” to a number array?

Use str.split(",").map(Number), then filter with Number.isFinite to drop NaN.

What type does JSON.parse return in TypeScript?

It returns any. Annotate the variable or, safer, validate the value with a type guard.

Where is String.prototype.split documented?