JavaScript Array toString()

Posted by

Method

JavaScript Array toString() Method

The Array.prototype.toString() method is used to convert an array into a string. It returns a string representation of the array and its elements.

Syntax

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

array.toString()

[/dm_code_snippet]

Parameters

The method does not take any parameters

Return Value

The method returns a string representation of the array and its elements.

Description

The Array.prototype.toString() method is used to convert an array into a string. It returns a string representation of the array and its elements, with each element separated by a comma (,).

The toString() method is called automatically when an array is used in a string context. This means, that an array can be passed as an argument to a function, and the function can print the array without having to explicitly call the toString() method.

Example

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

var arr = [1, 2, 3];

console.log(arr.toString());
// output: "1,2,3"

[/dm_code_snippet]

In the above example, the toString() method is used to convert the array into a string. The toString() method is called on the array, and the string is printed out to the console.

Browser Support

The toString() method is supported in all major browsers.