Skip to content
Related Articles

Related Articles

Improve Article

JavaScript Array toString() Method

  • Last Updated : 25 Jun, 2020

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
  • Microsoft Edge
  • Mozilla Firefox
  • Safari
  • Opera



My Personal Notes arrow_drop_up
Recommended Articles
Page :