JavaScript Code

JavaScript Array join()

join() – Join Elements of Array

  • array is an array of elements
  • join is the array method name
  • separator is an optional string value. default value is comma

join() method joins the elements of the array with the given separator string, and returns the result as a string. join() method does not modify the original array


1. join the elements of the array fruits with '-' hyphen character as separator

let fruits = ['apple', 'banana', 'cherry'];
let separator = '-';
output = fruits.join(separator);

2. call join() method on fruits array with no separator passed

let fruits = ['apple', 'banana', 'cherry'];
let output = fruits.join();

since comma is the default separator, we get a comma separated elements in the resulting string

Copyright @2022