TypeScript Array toString()
Syntax & Examples

Array.toString() method

The toString() method returns a string representing the specified array and its elements.


Syntax of Array.toString()

The syntax of Array.toString() method is:

toString(): string

This toString() method of Array returns a string representation of an array.

Return Type

Array.toString() returns value of type string.



✐ Examples

1 Using toString() on a numeric array

The toString() method can be used to convert a numeric array to a string.

For example,

  1. Create an array arr with numeric values [1, 2, 3, 4, 5].
  2. Use toString() to convert the array to a string.
  3. Store the result in result.
  4. Log result to the console.

TypeScript Program

const arr = [1, 2, 3, 4, 5];
const result = arr.toString();
console.log(result);

Output

1,2,3,4,5

2 Using toString() on a string array

The toString() method can be used to convert an array of strings to a single string.

For example,

  1. Create an array arr with string values ['a', 'b', 'c'].
  2. Use toString() to convert the array to a string.
  3. Store the result in result.
  4. Log result to the console.

TypeScript Program

const arr = ['a', 'b', 'c'];
const result = arr.toString();
console.log(result);

Output

a,b,c

3 Using toString() on a mixed array

The toString() method can be used to convert an array with mixed types to a string.

For example,

  1. Create an array arr with mixed values [1, 'a', true, null].
  2. Use toString() to convert the array to a string.
  3. Store the result in result.
  4. Log result to the console.

TypeScript Program

const arr = [1, 'a', true, null];
const result = arr.toString();
console.log(result);

Output

1,a,true,

Summary

In this TypeScript tutorial, we learned about toString() method of Array: the syntax and few working examples with output and detailed explanation for each example.