How to use JavaScript Array join()

Array join

Array join() Method

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

  • Declare two variable a and b
  • Initialize two variable a and b with value of array a = [‘Cat’, ‘Dog’, ‘Tiger’, ‘Lion’, ‘Elephant’] and b = a.join(” “)
  • The array join() method joins the given statement to all elements of the array.
  • The value of b variable is displayed with the help of console.log inbuilt function

Array join() Method

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

  • Declare two variable c and d
  • Initialize two variable c and d with value of array c = [‘Good Morning’, ‘Good Afternoon’, ‘Good Evening’, ‘Good Night’] and d = c.join(” * “)
  • The array join() method joins the given statement to all elements of the array.
  • The value of d variable is displayed with the help of console.log inbuilt function