Skip to content
Related Articles

Related Articles

Improve Article

How to create a string by joining the elements of an array in JavaScript ?

  • Last Updated : 24 Mar, 2021

Given an array containing array elements and here we will join all the array elements to make a single string. To join the array elements we use arr.join() method. This method is used to join the elements of an array into a string. The elements of the string will be separated by a specified separator and its default value is a comma(,).

Syntax:

array.join(separator) 

In this case, we use empty separator i.e. array.join(“”) to joint the array elements.

Example:

HTML




<!DOCTYPE html>
<html>
  
<head>
    <title>
        How to create a string by joining the
        elements of an array in JavaScript?
    </title>
</head>
  
<body style="text-align: center;">
    <h1 style="color:green;">
        GeeksforGeeks
    </h1>
  
    <h3>
        How to create a string by joining the<br>
        elements of an array in JavaScript?
    </h3>
  
    <h4>Original Array: ["Welcome", "Geeks", "for", "Geeks"]</h4>
  
    <button onclick="geeks()">Click Here!</button>
  
    <h4 id="result"></h4>
  
    <script>
        function geeks() {
            var str = ["Welcome", "Geeks", "for", "Geeks"];
  
            document.getElementById("result").innerHTML 
                = "Joined String: " + str.join("");
        }
    </script>
</body>
  
</html>

Output:

Attention reader! Don’t stop learning now. Get hold of all the important HTML concepts with the Web Design for Beginners | HTML  course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :