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 triali1276
7,626 PointsHow can the 2nd js file use array from 1st js file?
I was just wondering, what if directory.js also has a variable called "pet", what would happen then?
I thought we had to do some kind of import before using the array from another javascript file
1 Answer
Bob Swaney
13,010 PointsWhen using 'vanilla' JavaScript and placing a script tag under the first script tag in the .html file, it is allowing the browser to load the first contents of the first .js file (i.e. pets.js) thereby loading in the pets array. If for some reason both files had global variables that were named the same thing, then yes, there would be an Error thrown due to the conflicting names. In other types of JS libraries such as Express, there are import types of methods to use modules and pieces of scripts from one file into another; Here we are just using one file as a data structure and another as a small piece of code, so there aren't any conflicts.
Hope this helps!