Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Arrays Multidimensional Arrays Review JavaScript Arrays

Quiz Question 2 (What is the correct answer?)

Say you have two arrays, itemsA and itemsB, and you want to combine them into a new array named allItems. Complete the code so that the new allItems array stores the elements from itemsA first, followed by the elements inside itemsB:

const itemsA = [ 'Lightsaber', 'Mockingjay pin', 'Box of chocolates' ]; const itemsB = [ 'Ghost trap', 'The One Ring', 'DeLorean' ] const allItems = [ , ];

Abdi Salad
Abdi Salad
8,078 Points
const itemsA = [ 'Lightsaber', 'Mockingjay pin', 'Box of chocolates' ]; 
const itemsB = [ 'Ghost trap', 'The One Ring', 'DeLorean' ];
const alltems = itemsA.concat(itemsB);

this is how you would you do that. It's important to check out documentation. visiting the JavaScript array documentation would've saved you some time because it has useful methods there.

5 Answers

Thank you Abdi for your reply. I did google on how to combine the two arrays and I did find the answer that you provided to me but in this specific instance, it looks like the better solution was to use the spread operator to copy both arrays into the third array. That solved my challenge question. Thank you again for your reply.

I agree in fact, abdi answer is wrong given the problem being asked. it has a comma but abdi answer ignore the comma

For this problem, we use the spread operator(...) to join the two arrays.

const itemsA = [ 'Lightsaber', 'Mockingjay pin', 'Box of chocolates' ];
const itemsB = [ 'Ghost trap', 'The One Ring', 'DeLorean' ]
const allItems = [ ...itemsA , ...itemsB  ];

This is the correct solution. Use the spread operator. This worked for me. thank you Paul Oketch

...itemsA, ...itemsB

Manoj Gurung
Manoj Gurung
5,318 Points

all the answers donot provide the real answer requested. We gotta change this.

the answer will be ...itemsA, ...itemsB