Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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