JavaScript

June 5, 2023

How to use JavaScript Array concat()

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

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

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

  1. Declare four variables d, e, f and g
  2. 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)
  3. In Concat() array method, an array can be concatenated to more than one array.
  4. The value of g variable is displayed with the help of console.log inbuilt function

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

  1. Declare five variables h, i, j, k and l
  2. 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)
  3. In Concat() array method, an array can be concatenated to more than one array.
  4. The value of l variable is displayed with the help of console.log inbuilt function

by : Suhel Akhtar

Quick Summary:

JavaScript Array concat() method 2 array string example 1 array number example