Open In App

What is Array.join() method in JavaScript?

Last Updated : 14 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In JavaScript, the join() method is used to create and return a new string by concatenating all the elements of an array, separated by a specified separator string. If no separator is provided, a comma (,) is used as the default separator.

Syntax:

array.join(separator)

Parameter:

  • array: The array whose elements will be joined into a string.
  • separator (optional): A string used to separate each element in the resulting string. If omitted, a comma (,) is used as the separator.

Example: Here, the join() method is called on the fruits array with ", " as the separator. This joins all the elements of the array into a single string, with each element separated by ", ". The resulting string "apple, banana, orange" is stored in the result variable.

Javascript




const fruits = ["apple", "banana", "orange"];
const result = fruits.join(", ");
 
console.log(result);


Output

apple, banana, orange


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads