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 AJAX Basics (retiring) Programming AJAX Parsing JSON Data

Jose Linares
Jose Linares
5,062 Points

How many javascript files can a website have ?

I was just wondering, how many javascript files can a website have? Do I have to put all my code in a single file, or can I have a different .js file for each page on my website ?

3 Answers

jason chan
jason chan
31,009 Points

you will later learn gulp it will combine all of the js files into one. plus minify. Learn gulp.

malkio kusanagi
malkio kusanagi
12,336 Points

Hey man, you are free to separate javascript files, but you need to consider that every

<script src="my_javascript.js"></script>
<script src="my_javascript_2.js"></script>
<script src="my_javascript_3.js"></script>
<script src="my_javascript_4.js"></script>

is equivalent to a new HTTP call, means it could affect the page load speed

You can also take a look at javascript task runner grunt to compile your separated javascript files into one script file.

Jose Linares
Jose Linares
5,062 Points

yeah but what if I have a page that needs some javascript and another one that does not need it?

malkio kusanagi
malkio kusanagi
12,336 Points

If you have javascript codes that only needs to run on specific pages, then there is no reason including it on other pages. While you need to keep general javascript codes into one file to avoid repeating the same codes on you other external js files

Jose Linares
Jose Linares
5,062 Points

OK thank you both, I'll look into Gulp next Jason Chan. Thanks

jason chan
jason chan
31,009 Points

No problemo bro. goodluck.