JavaScript join() method
JavaScript join() Method
Instance
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> </head> <body> <p id="demo">点击按钮将数组作为字符串输出。</p> <button onclick="myFunction()">点我</button> <script> function myFunction(){ var fruits = ["Banana", "Orange", "Apple", "Mango"]; var x=document.getElementById("demo"); x.innerHTML=fruits.join(); } </script> </body> </html>
Run Instance»
Click the "Run Example" button to view the online example
Definition and usage
The join() method is used to convert all elements in the array into a string.
Elements are separated by the specified delimiter.
Browser support
All major browsers support the join() attribute.
Syntax
##array.join(separator)
Description | |
---|---|
separator | Optional. Specify the delimiter to use. If this parameter is omitted, a comma is used as the delimiter.
Description | |
---|---|
Returns a string. The string is generated by converting each element of the arrayObject to a string and then concatenating the strings, inserting the separator string between the two elements. |
1.1 |
---|
More examples
Instances
Running examples»Click the "Run Instance" button to view the online instance
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> </head> <body> <p id="demo">点击按钮将数组作为字符串输出。</p> <button onclick="myFunction()">点我</button> <script> function myFunction(){ var fruits = ["Banana", "Orange", "Apple", "Mango"]; var x=document.getElementById("demo"); x.innerHTML=fruits.join(" and "); } </script> </body> </html>
Running examples»Click the "Run Instance" button to view the online instance