JavaScript | Array toString()

arr.toString() returns the string representation of the array elements. The syntax of this function is as follows:

arr.toString()

Arguments
This function does not take any arguments.

Return value
The function returns the string representation of the array elements. If the array is empty, then it returns an empty string.



Example for the above function is provided below:

Example 1:

var arr = [2, 5, 8, 1, 4]
print(arr.toString());

Output:

2,5,8,1,4

In this example the function toString() creates a string consisting of array elements.

Code for the above function is provided below:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
// JavaScript to illustrate toString() function
function func() {
  
    // Original array
    var arr = [2, 5, 8, 1, 4];
  
    // Creating a string
    var str = arr.toString();
    document.write(str);
}
func();
</script>

chevron_right


Output:

2,5,8,1,4


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.