In JavaScript, the toString() method is used to convert an array to a string. This method returns a string representing the elements of the array separated by commas. The toString() method provides a simple way to convert arrays into strings, making it useful for various formatting and serialization tasks in JavaScript.
Syntax:
array.toString()
Example:
let fruits = ['apple', 'banana', 'orange'];
let stringRepresentation = fruits.toString();
console.log(stringRepresentation); // Output: "apple,banana,orange"
Explanation:
- The toString() method joins the elements of the array into a single string, with each element separated by a comma.
- If an element is an object, the toString() method of that object is called to convert it to a string.