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


Micole Noddle
Micole Noddle
5,797 Points

How to link JavaScript files to HTML properly?

How many JS files can one link? I remember Dave saying only to use two, but I could be off, and I went back through the courses to try and find the answer, but couldn't seem to.

For instance, let's say we have a JS folder inside our main project folder and then an "app.js" file and a "students.js" file inside the JS folder. First of all, would this be common practice (to have two or more files in the JS folder)? Second of all, how would we link it to the HTML? Would we write two lines of code before the closing body tag such as:

<script src = "js/app.js"></script>

<script src = "js/students.js"></script>

Is this correct? Incorrect? Any help would be greatly appreciated. Thanks so much!

2 Answers

Your syntax is perfectly fine. And to answer your question, Yes, you can link more than 2 js files. For my project I had linked nearly 10 js files. Actually what you are doing here you are importing the javascript code to the html page. So the more js files you link the more will be the page load time.

Isaac Sanchez
Isaac Sanchez
3,126 Points

When linking JavaScript files you can have as many as you need. If they are in a folder maker sure you specify which one, like you did. You also don't have to write your code just at the end of the body tag, placing them at the end of the <head> tag is common.