How to concatenate three arrays in JavaScript?

By IncludeHelp Last updated : January 20, 2024

Problem statement

Given three JavaScript Arrays, you have to concatenate them and store the returns in a fourth array.

Concatenating three arrays

To concatenate three JavaScript arrays, you can use the Array.concat() method. Call the concat() method with one array and pass the second and third arrays as the parameters, it will return a concatenate array, and store the result to another array.

JavaScript code to concatenate three arrays

The following example concatenates three arrays cities, temperatures, and area.

// declaring three arrays
const cities = ["New Delhi", "Mumbai", "Indore"];
const temperatures = [30, 18, 16];
const area = [48, 150, 78];

// concating arrays (cities, temperatures, and area)
const result = cities.concat(temperatures, area)

// printing the arrays
console.log("cities:", cities);
console.log("temperatures:", temperatures);
console.log("area:", area);
console.log("result:", result);

Output

The output of the above code is:

cities: ['New Delhi', 'Mumbai', 'Indore']
temperatures: [30, 18, 16]
area: [48, 150, 78]
result: ['New Delhi', 'Mumbai', 'Indore', 30, 18, 16, 48, 150, 78]

To understand the above example, you should have the basic knowledge of the following JavaScript topics:

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.