Below is the example of the Array join() method.
- Example:
<script>functionfunc() {vara = [ 1, 2, 3, 4, 5, 6 ];document.write(a.join('|'));}func();< /script>chevron_rightfilter_none - Output:
1|2|3|4|5|6
The arr.join() 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)
Parameters: This method accept single parameter as mentioned above and described below:
- separator: It is Optional i.e, it can be either used as parameter or not.Its default value is comma(, ).
- Example 1: In this example the function join() joins together the elements of the array into a string using ‘|’.
var a = [1, 2, 3, 4, 5, 6]; print(a.join('|'));Output:
1|2|3|4|5|6
- Example 2: In this example the function join() joins together the elements of the array into a string using ‘, ‘ since it is the default value.
var a = [1, 2, 3, 4, 5, 6]; print(a.join());
Output:
1, 2, 3, 4, 5, 6
- Example 3: In this example the function join() joins together the elements of the array into a string using ‘ ‘ (empty string).
var a = [1, 2, 3, 4, 5, 6]; print(a.join(''));Output:
123456
Return Value: It returns the String which contain the collection of array’s elements.
Below example illustrate the Array join() method in JavaScript:
Code for the above method is provided below:
Program 1:
<script> function func() { var a = [ 1, 2, 3, 4, 5, 6 ]; document.write(a.join()); } func(); </script> |
Output:
1, 2, 3, 4, 5, 6
Program 2:
<script> function func() { var a = [ 1, 2, 3, 4, 5, 6 ]; document.write(a.join('')); } func(); </script> |
Output:
123456
Supported Browsers: The browsers supported by JavaScript Array join() method are listed below:
- Google Chrome 1.0
- Microsoft Edge 5.1
- Mozilla Firefox 1.0
- Safari
- Opera
Recommended Posts:
- JavaScript | Array join() function
- JavaScript | typedArray.join() with Example
- TypeScript | Array join() Method
- Node.js | path.join() Method
- Lodash _.join() Method
- PHP | join() Function
- PHP | Ds\Sequence join() Function
- PHP | Ds\Vector join() Function
- PHP | Ds\Deque join() Function
- p5.js | join() function
- PHP | Ds\Set join() Function
- Node.js | join() function
- How to use .join() with push and replace together?
- How to compare two JavaScript array objects using jQuery/JavaScript ?
- JavaScript Course | Understanding Code Structure in JavaScript
- Introduction to JavaScript Course | Learn how to Build a task tracker using JavaScript
- JavaScript Course | Loops in JavaScript
- JavaScript Course | Data Types in JavaScript
- JavaScript Course | Printing Hello World in JavaScript
- JavaScript Course | Logical Operators in JavaScript
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : nidhi_biet


