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 Objects Loop Through Objects Display an Array of Objects on the Page – One Solution

Christopher King
Christopher King
4,665 Points

Attaching two javascript files

Hi,

I noticed that Guil references a variable from the first .js file from the second .js file to loop through the array. It did not occur to me at all to create a second js file for this quiz. Is this standard practice?

2 Answers

Although for this you can do it in one file but for big projects, you would prefer each component in a separate file.

And, If you will not separate, it will be very tedious to manage.

Also, It's a good practice.

Hi Christopher!

Guil's JS code could all be in one long file and it would work just as well. I think the main reasoning in separating certain JS code into separate files is to logically cluster related JS code for easier readability and to make parts of the code easier to pinpoint when debugging.

I usually end up with multiple JS files when I borrow other people's code samples from places like CodePen, for, say, a lightbox or slider. Which means, in addition to having a main JS file for my site, I'll also likely have a lightbox.js file and slider.js file (as well as, most likely a lightbox.css file and a slider.css file).

Does that make sense?

I hope that helps.

Stay safe and happy coding!