How to use JavaScript Array concat()

Array concat

Array concat() Method

Program to use JavaScript Array concat() method

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

Output

[ 'Cat', 'Dog', 'Tiger', 'Lion', 'Elephant' ]
    

Steps Description

  • Declare three variables a, b and c
  • Initialize three variables with array value respectively a = [‘Cat’, ‘Dog’], b = [‘Tiger’, ‘Lion’, ‘Elephant’] and c = a.concat(b)
  • In Concat() array method, an array can be concatenated to more than one array.
  • The value of c variable is displayed with the help of console.log inbuilt function

Array concat() Method

Program to use JavaScript Array concat() method

let d = ['Peas', 'Carrot', 'Potato'];
    let e = ['Tomato', 'Brinjal', 'Cabbage'];
    let f = ['Green Chilli', 'Green Coriander']
    let g = d.concat(e,f);
    console.log(g);
    

Output

[
      'Peas',
      'Carrot',
      'Potato',
      'Tomato',
      'Brinjal',
      'Cabbage',
      'Green Chilli',
      'Green Coriander'
    ]
    

Steps Description

  • Declare four variables d, e, f and g
  • Initialize four variables with array value respectively d = [‘Peas’, ‘Carrot’, ‘Potato’], e = [‘Tomato’, ‘Brinjal’, ‘Cabbage’], f = [‘Green Chilli’, ‘Green Coriander’] and g = d.concat(e,f)
  • In Concat() array method, an array can be concatenated to more than one array.
  • The value of g variable is displayed with the help of console.log inbuilt function

Array concat() Method

Program to use JavaScript Array concat() method

let h = [1,2,3];
    let i = [4,5];
    let j = [6,7,8];
    let k = [9,10];
    let l = h.concat(i,j,k);
    console.log(l);
    

Output

[
      1, 2, 3, 4,  5,
      6, 7, 8, 9, 10
    ]
    

Steps Description

  • Declare five variables h, i, j, k and l
  • Initialize five variables with array value respectively h = [1,2,3], i = [4,5], j = [6,7,8], k = [9,10] and l = h.concat(i,j,k)
  • In Concat() array method, an array can be concatenated to more than one array.
  • The value of l variable is displayed with the help of console.log inbuilt function