JavaScript implements the method of concatenating all the elements of an array into a single string

  • 2020-05-26 07:49:04
  • OfStack

This article demonstrates how the JavaScript implementation concatenates all the elements of an array into a single string. Share with you for your reference. The details are as follows:

The following code demonstrates how to concatenate array elements into 1 string output in JS via the join method of array objects


<!DOCTYPE html>
<html>
<body>
<p id="demo">
Click the button to join the array elements into a string.
</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction()
{
var fruits = ["Banana","Orange","Apple","Mango"];
var x=document.getElementById("demo");
x.innerHTML=fruits.join();
}
</script>
</body>
</html>

The results are as follows:

Banana,Orange,Apple,Mango

I hope this article has been helpful to your javascript programming.


Related articles: