Home »
JavaScript »
JavaScript Examples
How to concatenate two arrays in JavaScript?
By IncludeHelp Last updated : January 20, 2024
Problem statement
Given two JavaScript Arrays, you have to concatenate them and store the returns in a third array.
Concatenating two arrays
To concatenate two JavaScript arrays, you can use the Array.concat() method. Call the concat() method with one array and pass the second array as a parameter, it will return a concatenate array, and store the result to another array.
JavaScript code to concatenate two arrays
The following example concatenates two arrays cities and temperatures.
// declaring two arrays
const cities = ["New Delhi", "Mumbai", "Indore"];
const temperatures = [30, 18, 16];
// concating arrays (cities and temperatures)
const result = cities.concat(temperatures)
// printing the arrays
console.log("cities:", cities);
console.log("temperatures:", temperatures);
console.log("result:", result);
Output
The output of the above code is:
cities: ['New Delhi', 'Mumbai', 'Indore']
temperatures: [30, 18, 16]
result: ['New Delhi', 'Mumbai', 'Indore', 30, 18, 16]
To understand the above example, you should have the basic knowledge of the following JavaScript topics: