Javascript Array toString()

The toString() method returns a string formed by the elements of the given array.

Example


toString() Syntax

The syntax of the toString() method is:

arr.toString()

Here, arr is an array.


toString() Parameters

The toString() method does not take any parameters.


toString() Return Value

  • Returns a string representing the values of the array separated by a comma

Notes:

  • The toString() method does not change the original array.
  • Elements like undefined, null, or empty array, have an empty string representation.

Example 1: Using toString() Method

Output

Terence,28,Kathmandu
[ 'Terence', 28, 'Kathmandu' ]

In the above example, we have used the toString() method to convert all the elements of the info array into a string.

info.toString() returns the string representation of info which is Terence,28,Kathmandu.

Since the method does not change the original array, the info array holds the same original value.


Example 2: toString() with Nested Arrays

When we use the toString() method in a nested array, the array is flattened. For example:

Output

1,2,4,Apple,5

Recommended Readings: