The toString() method of the Array class in JavaScript returns a string representing the calling array and its elements. It overrides the Object.prototype.toString() method.
The syntax of Array.toString() method is:
toString()
This toString() method of Array returns a string representing the calling array and its elements. Overrides the Object.prototype.toString() method.
Array.toString() returns value of type String
.
In JavaScript, we can use the toString() method to convert an array to a string representation.
For example,
const arr = [1, 2, 3, 4, 5];
const stringRepresentation = arr.toString();
console.log(stringRepresentation);
1,2,3,4,5
We can also use the toString() method on an array of strings to get a string representation.
For example,
const strArr = ['apple', 'banana', 'cherry', 'date'];
const stringRepresentation = strArr.toString();
console.log(stringRepresentation);
apple,banana,cherry,date
In this JavaScript tutorial, we learned about toString() method of Array: the syntax and few working examples with output and detailed explanation for each example.