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

rocky roy
rocky roy
556 Points

How can i concat 3 arrays together.

i understood how to add to array together but how can i add there arrays into on arrays .

2 Answers

You can concatenate three arrays using the concat() method like this:

var hege = ["Cecilie", "Lone"];
var stale = ["Emil", "Tobias", "Linus"];
var car = ["audi", "bmw", "hundia"];

var alltogether = hege.concat(stale, car);

Now alltogether contains all three array's contents.

rocky roy
rocky roy
556 Points

i know this but i wants connect 3 arrays. EG: var hege = ["Cecilie", "Lone"]; var stale = ["Emil", "Tobias", "Linus"]; var car = ["audi", "bmw", "hundia"];

Like Atilla Mentioned that would be the correct way to concatenate arrays but you could also do

var hege = ["Cecilie", "Lone"]; 
var stale = ["Emil", "Tobias", "Linus"];
var car = ["audi", "bmw", "hundia"];

var array = [hege,stale,car]; // Output is Cecilie,Lone,Emil,Tobias,Linus,audi,bmw,hundia

It's pretty much the same thing as using concat() but if you can tell me what your applying this to maybe I can definitely help out some more :D