Convert array to string in typescript

To convert array to string in typescript, use the join() method it will convert the array to a comma (,) separated string.

The join() method creates and returns a new string by concatenating all of the elements in an array (or an array-like object), separated by commas or a specified separator string. If the array has only one item, then that item will be returned without using the separator.

const arr: Array<string> = ["infinitbility","aguidehub","sortoutcode"];

// โœ… Convert array to string
const str1: string = arr.join();
console.log(str1); // ๐Ÿ‘‰๏ธ 'infinitbility,aguidehub,sortoutcode'

const str2: string = arr.join('');
console.log(str2); // ๐Ÿ‘‰๏ธ 'infinitbilityaguidehubsortoutcode'

const str3: string = arr.join('-');
console.log(str3); // ๐Ÿ‘‰๏ธ 'infinitbility-aguidehub-sortoutcode'

Convert an array of object to string in typescript

To convert an array of objects to string in typescript, use JSON.stringify() it will convert your array of objects to string.

The JSON.stringify() method converts a JavaScript object or value to a JSON string, optionally replacing values if a replacer function is specified or optionally including only the specified properties if a replacer array is specified.

interface domain {
    name: string,
    domain: string,
}

const arr: Array<domain> = [
        {
            name: "infinitbility",
            domain: "infinitbility.com"
        },
        {
            name: "aguidehub",
            domain: "aguidehub.com"
        }
    ];

// โœ… Convert array of object to string
const str: string = JSON.stringify(arr);
console.log(str); // ๐Ÿ‘‰๏ธ '[{"name":"infinitbility","domain":"infinitbility.com"},{"name":"aguidehub","domain":"aguidehub.com"}]'

Note: The toString() and join() method only work for array of string, number not object.

I hope it helps you, All the best ๐Ÿ‘.