Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Combining Arrays with concat and spread | Advanced Array Manipulation
JavaScript Array Methods

bookCombining Arrays with concat and spread

Combining arrays is a common task when working with data in JavaScript. You might need to merge lists of items, concatenate results from different sources, or simply reorganize your data. Two primary ways to combine arrays are the concat method and the spread operator (...). Both approaches let you join arrays into a single new array, but they have different syntax and offer unique advantages in certain situations.

12345678910
// Using concat to merge two arrays const fruits = ["apple", "banana"]; const vegetables = ["carrot", "lettuce"]; const food = fruits.concat(vegetables); console.log(food); // ["apple", "banana", "carrot", "lettuce"] // Using the spread operator to merge arrays const drinks = ["water", "juice"]; const allItems = [...fruits, ...vegetables, ...drinks]; console.log(allItems); // ["apple", "banana", "carrot", "lettuce", "water", "juice"]
copy

The concat method is straightforward and easy to read, making it a good choice when you want to merge two or more arrays without modifying the originals. The spread operator, on the other hand, provides greater flexibility. With spread, you can insert arrays anywhere within a new array, combine more than two arrays at once, and even add individual elements directly alongside arrays. This flexibility often makes the spread operator more readable, especially when dealing with complex array structures or when you want to combine arrays with other values.

Note

When you combine arrays using either concat or the spread operator, the result is a shallow copy. This means the top-level elements are copied into a new array, but if your arrays contain objects or other arrays, those nested references remain shared. Modifying a nested object in the combined array will also affect the original arrays. Understanding this behavior is important to avoid unexpected side effects when working with complex data structures.

1. Which method would you choose if you need to combine three arrays and add a new element at the beginning of the result?

2. Fill in the blanks to merge the arrays a, b, and c using the spread operator so that the resulting array starts with all elements from a, followed by b, then c.

question mark

Which method would you choose if you need to combine three arrays and add a new element at the beginning of the result?

Select the correct answer

question-icon

Fill in the blanks to merge the arrays a, b, and c using the spread operator so that the resulting array starts with all elements from a, followed by b, then c.

const a = [1, 2]; const b = [3, 4]; const c = [5, 6]; const combined = [, , ];
[1, 2, 3, 4, 5, 6]

Click or drag`n`drop items and fill in the blanks

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 2

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

Can you explain what a shallow copy means in this context?

When should I use concat versus the spread operator?

Are there ways to deeply clone arrays that contain objects?

Awesome!

Completion rate improved to 8.33

bookCombining Arrays with concat and spread

Swipe to show menu

Combining arrays is a common task when working with data in JavaScript. You might need to merge lists of items, concatenate results from different sources, or simply reorganize your data. Two primary ways to combine arrays are the concat method and the spread operator (...). Both approaches let you join arrays into a single new array, but they have different syntax and offer unique advantages in certain situations.

12345678910
// Using concat to merge two arrays const fruits = ["apple", "banana"]; const vegetables = ["carrot", "lettuce"]; const food = fruits.concat(vegetables); console.log(food); // ["apple", "banana", "carrot", "lettuce"] // Using the spread operator to merge arrays const drinks = ["water", "juice"]; const allItems = [...fruits, ...vegetables, ...drinks]; console.log(allItems); // ["apple", "banana", "carrot", "lettuce", "water", "juice"]
copy

The concat method is straightforward and easy to read, making it a good choice when you want to merge two or more arrays without modifying the originals. The spread operator, on the other hand, provides greater flexibility. With spread, you can insert arrays anywhere within a new array, combine more than two arrays at once, and even add individual elements directly alongside arrays. This flexibility often makes the spread operator more readable, especially when dealing with complex array structures or when you want to combine arrays with other values.

Note

When you combine arrays using either concat or the spread operator, the result is a shallow copy. This means the top-level elements are copied into a new array, but if your arrays contain objects or other arrays, those nested references remain shared. Modifying a nested object in the combined array will also affect the original arrays. Understanding this behavior is important to avoid unexpected side effects when working with complex data structures.

1. Which method would you choose if you need to combine three arrays and add a new element at the beginning of the result?

2. Fill in the blanks to merge the arrays a, b, and c using the spread operator so that the resulting array starts with all elements from a, followed by b, then c.

question mark

Which method would you choose if you need to combine three arrays and add a new element at the beginning of the result?

Select the correct answer

question-icon

Fill in the blanks to merge the arrays a, b, and c using the spread operator so that the resulting array starts with all elements from a, followed by b, then c.

const a = [1, 2]; const b = [3, 4]; const c = [5, 6]; const combined = [, , ];
[1, 2, 3, 4, 5, 6]

Click or drag`n`drop items and fill in the blanks

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 2
some-alt