Combine arr1 and arr2

The following code works fine as arr2 is empty, and arr2 got the contents of arr1, but I’m really confused that if arr2 is not empty, like arr2 = [‘JUNE’, ‘JULY’], anybody can tell me how I can put arr1 and arr2 together? I hope I’ve made me clear, thanks.

Your code so far

const arr1 = ['JAN', 'FEB', 'MAR', 'APR', 'MAY'];
let arr2;

arr2 = [...arr1]; 


Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.84 Safari/537.36

Challenge: Use the Spread Operator to Evaluate Arrays In-Place

Link to the challenge:

This line here says "ignore the contents of arr2 and replace it with a shallow copy of arr1"

1 Like

As it stands, your code is replacing the value in arr2. But if you modified the [...arr1] to include arr2 somehow… :thinking:

1 Like

There are a couple of ways to join arrays in JS. Here are 2 common ways:

  1. let arr3 = arr2.concat(arr1)
    // does not modify arr1 and arr2

  2. arr2 = [...arr2, ...arr1]
    // modifies arr2

  3. arr3 = [...arr1, ...arr2]
    // does not modify arr1 and arr2


Thanks, it works :grinning: :grinning: :grinning:

1 Like

technique1: arr1.concat(arr2)
technique2: const arr2 = [a, b, c, …arr1]

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.