The toString()
method is used to convert an array to a string of comma-separated array values. In other words, the method returns a string that represents the specified array and its items or elements.
array.toString()
This method accepts no parameters.
toString()
returns a string that represents the array elements. If the array is empty, then toString()
returns an empty string.
The example below shows how to use the toString()
method on an array:
// create arrayslet array1 = [1, 2, 3, 4, 5];let array2 = ["a", "b", "c", "d", "e"]let array3 = ["Ruby", "Python", "Javascript", "Java"]let array4 = ["", {name: "toString()"}, NaN, 0]// call the toString() methodlet A = array1.toString();let B = array2.toString();let C = array3.toString();let D = array4.toString();// print returned value to consoleconsole.log(A);console.log(B);console.log(C);console.log(D);
Note: An empty array will return nothing if
toString()
is called.
// create empty arraylet emptyArray = []// print the returned value of// emptyArray when toString() is called on itconsole.log(emptyArray.toString()); // Empty string returned