How to use JavaScript Array join()

Program to use JavaScript Array join() method


let a = ['Cat', 'Dog', 'Tiger', 'Lion', 'Elephant'];
let b = a.join(" ");
console.log(b);

Output


Cat Dog Tiger Lion Elephant

Steps Description

  1. Declare two variable a and b
  2. Initialize two variable a and b with value of array a = ['Cat', 'Dog', 'Tiger', 'Lion', 'Elephant'] and b = a.join(" ")
  3. The array join() method joins the given statement to all elements of the array.
  4. The value of b variable is displayed with the help of console.log inbuilt function

Program to use JavaScript Array join() method


let c = ['Good Morning', 'Good Afternoon', 'Good Evening', 'Good Night'];
let d = c.join(" * ");
console.log(d);

Output


Good Morning * Good Afternoon * Good Evening * Good Night

Steps Description

  1. Declare two variable c and d
  2. Initialize two variable c and d with value of array c = ['Good Morning', 'Good Afternoon', 'Good Evening', 'Good Night'] and d = c.join(" * ")
  3. The array join() method joins the given statement to all elements of the array.
  4. The value of d variable is displayed with the help of console.log inbuilt function