JavaScript | Array.join() Method

Introduction:
The array.join() method is an inbuilt function in JavaScript which 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 comma(, ).
Syntax:

array.join(separator)

    Parameters:

  • separator: It is Optional i.e, it can be either used as parameter or not.Its default value is comma(, ).
  • Return Value:



  • It returns the String which contain the collection of array’s elements.
  • JavaScript Version:

  • ECMAScript 1
  • Browser Support:

  • Here 2nd column contained int values are verson of the corresponding browser.
    Feature Basic support
    Chrome 1
    Edge Yes
    Firefox 1
    Internet Explorer 5.5
    Opera Yes
    Safari Yes
    Android webview Yes
    Chrome for Android Yes
    Edge mobile Yes
    Firefox for Android 4
    Opera Android Yes
    iOS Safari Yes

Examples:

Input: elements = [\'geeksforgeeks\', \'gfg\']
Output: \"geeksforgeeks, gfg\"

Explanation:
Here input contains some elements as an array. And output becomes the string of elements of the array.
This is done with the help of Array.join() method in JavaScript.


Let\’s see JavaScript program:

filter_none

edit
close

play_arrow

link
brightness_4
code

// input array contains some elements to be joined.
var elements = [\'geeksforgeeks\', \'gfg\'];
  
// Here join() function joins the elements of the array.
console.log(elements.join());
  
// Here output elements are seperated by dot (.).
console.log(elements.join(\'.\'));
  
// Here output elements are seperated by hyphen (-).
console.log(elements.join(\'-\'));

chevron_right


Output:

> \"geeksforgeeks, gfg\"
> \"geeksforgeeks.gfg\"
> \"geeksforgeeks-gfg\"

    Application:

  • Whenever we need to get elements of any array that time we take help of Array.join() Method in JavaScript.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // input array contains some elements to be joined.
    var elements = [\'geeksforgeeks\', \'gfg\'];
      
    // Here output elements are seperated by addition symbol (+).
    console.log(elements.join(\'+\'));

    chevron_right

    
    

    Output:

    > \"geeksforgeeks+gfg\"
    


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.