Skip to content

JavaScript concat two Arrays | Example code

  • by

Use the concat() method to concat (joins) two or more arrays in JavaScript. This method returns a new array, containing the joined arrays. It does not change the original arrays.

array1.concat(array2)

Example concat two Arrays in JavaScript

Simple example code.

<!DOCTYPE html>
<html>
<head>

  <script>

    const arr1 = ["Apple", "Orange"];
    const arr2 = ["Cherry", "Kiwi"];
    const res = arr1.concat(arr2);

    console.log(res)
  </script>

</head>
</html>

Output:

JavaScript concat two Arrays

More example

var a = ['a','b','c'];
var b = ['d','e','f'];

var c = a.concat(b); //c is now an an array with: ['a','b','c','d','e','f']

console.log( c[3] ); //c[3] will be 'd'

Using modern JavaScript syntax – spread operator:

const a = ['a', 'b', 'c'];
const b = ['d', 'e', 'f'];

const c = [...a, ...b]; // c = ['a', 'b', 'c', 'd', 'e', 'f']

It is also the fastest way to concatenate arrays in JavaScript today.

Do comment if you have any doubts or suggestions on this JS Concat Arrays.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading