Latest Code Tutorials

Javascript Array ToString: How to Use Array.prototype.toString()

Javascript Array toString is an inbuilt function that returns a string representing the specified array and its elements. The toString() method converts an array into a String and returns the result. For Array objects, the toString() method joins an array and returns one string that containing each array element separated by the commas.

Javascript array toString()

The toString() method returns a string with all the array values, separated by commas and it does not modify the original array.


The syntax of the Javascript Array ToString() method is the following.


Let’s take an example.

// app.js

let arr = ['krunal', 'ankit', 'rushabh', 'dhaval'];

The output is the following.

Javascript Array ToString Example

So, that means it has returned the comma-separated values.

JavaScript calls a toString() method automatically when the array is to be represented as the text value or when the array is referred to in a string concatenation.

The returned string will separate the elements in the array with commas.

A slightly less common approach is to use the Javascript Array join() method. By default, it adds the comma to resulting values, though you can also specify that it adds nothing or some other separator value.

Javascript Array join() method creates and returns the new string by concatenating all of the elements of an array.

You can also take advantage of JavaScript’s type coercion: the + operator can either add the two numbers or concatenate the two strings.

But what happens when we try to join the object (the array [] is also the object) with something else? JavaScript is forgiving so it will not crash the program.

Instead of that, it does know how to concatenate the strings so that it will convert everything into the string.

Convert Array of Arrays to String

We can also convert an array of arrays into a String. Let’s see the following example.

// app.js

let arr = [['krunal', 'ankit', ['hello']], ['rushabh', 'dhaval']];

The output will be a single string with all the items comma separated. Notice that also, subarrays are separated by a comma as well.

See the below output.

Convert Array of Arrays to String

Finally, Javascript Array ToString Example is over.

1 Comment
  1. Amit Parmar says

    thanks to the post

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.