How to create a string by joining the elements of an array in JavaScript ?
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)
Example: In this case, we use an empty separator i.e. array.join(“”) to join the array elements.
HTML
<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> |
Output:



Please Login to comment...