split a string into an array of characters in typescript

You can split a string into an array of characters in TypeScript using the split() method as shown below:

index.ts
let str: string = "Hello World!";

let charArray: string[] = str.split("");

console.log(charArray); // ['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', '!']
165 chars
6 lines

In the above code, the split method is used on the str variable with an empty string "" as its argument. This splits the string at every character and returns an array of characters, which is then assigned to the charArray variable.

Finally, the charArray variable is logged to the console, which outputs an array of characters as expected.

gistlibby LogSnag