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 Loops, Arrays and Objects Tracking Data Using Objects The Build an Object Challenge, Part 2 Solution

Kevin Chau
Kevin Chau
5,874 Points

Does the first Javascript file take priority over the second Javascript file in the HTML file?

In the HTML file, does the order of <script src="javascript file"> matter? If the lines were switched would the program not run?

Kevin Chau
Kevin Chau
5,874 Points

Thanks for replies! I was assumed mtch's answer and Marcus's answer is insightful. I appreciate it!

Anytime, Kevin! We are here to help! :)

1 Answer

The order does matter, say for instance you have a jquery.min file and a global.js file where your code lives - you want to have jquery on top of global.js so that global.js sees jquery.min and uses it.

Hopefuly this helps.

And just to go in further on this, the javascript file that is lower on the page is going to take priority over the one that is higher. So, if you have 2 scripts on the page, the 2nd script you place on the page can overwrite the first one.

For example: let's say I have two scripts, and the first one makes a function called "atimesb" that returns 6. The second one defines a function called "atimesb" (exact same name) but this one returns 16. I then call console.log to show the output. Which does it show, 6 or 16? Well, the second function was declared after the first one so it overwrote the first one, so that it means it's going to return 16:

        <script>
            function atimesb () {
                return 3 * 2;
            }
        </script>
        <script>
            function atimesb () {
                return 4 * 4;
            }
//Returns 16 because atimesb function is overwritten
console.log(atimesb());
        </script>