下面的代碼演示了JS中如何通過數組對象的join方法將數組元素連接成一個字符串輸出
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 <!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>運行結果如下:
Banana,Orange,Apple,Mango