JavaScript Array toString() Method

Below is the example of Array toString() method.

  • Example:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <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>                   

    chevron_right

    
    

  • 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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<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>

chevron_right


Output:

2,5,8,1,4

Supported Browsers: The browsers supported by JavaScript Array toString() method are listed below:

  • Google Chrome
  • Microsoft Edge
  • Mozilla Firefox
  • Safari
  • Opera

full-stack-img

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.