JavaScript Array toString() Method
Below is the example of Array toString() method.
- Example:
<script>
// JavaScript to illustrate toString() method
function
func() {
// Original array
var
arr = [
"Geeks"
,
"for"
,
"Geeks"
];
// Creating a string
var
str = arr.toString();
document.write(str);
}
func();
</script>
- Output:
Geeks,for,Geeks
The arr.toString() method returns the string representation of the array elements.
Syntax:
arr.toString()
Parameters: This method does not accept any parameter.
Return value The method returns the string representation of the array elements. If the array is empty, then it returns an empty string.
Below examples illustrate the JavaScript Array toString() method:
- Example 1: In this example the toString() method creates a string consisting of array elements.
var arr = [2, 5, 8, 1, 4] print(arr.toString());
Output:
2,5,8,1,4
Code for the above method is provided below:
Program 1:
<script> // JavaScript to illustrate toString() method function func() { // Original array var arr = [2, 5, 8, 1, 4]; // Creating a string var str = arr.toString(); document.write(str); } func(); </script> |
Output:
2,5,8,1,4
Supported Browsers: The browsers supported by JavaScript Array toString() method are listed below:
- Google Chrome 1 and above
- Edge 12 and above
- Firefox 1 and above
- Internet Explorer 4 and above
- Opera 4 and above
- Safari 1 and above