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 Basics (Retired) Introducing JavaScript The Console Challenge Answer

Console.log placement in exercise

We originally placed the console.log in the html file during the learning videos. When we did the practice video at the end I placed the console.log statements around the <script src...> statement at the end of the body section in the html file. It does not state in the video if this would be an acceptable alternative way of doing this. The end result was the same, my statements appeared in the log at the right time....so just wondering if there is a best practice for this to know if need to place in js file or the html file? Thank you for your help. code in html file: <script> console.log("Start program"); </script> <script src="scripts.js"></script> <script> console.log("End program"); </script>

1 Answer

Dan Weru
Dan Weru
47,649 Points

Hi,

The best practice is to write your javascript in an external javascript file, then link to that file in your html. The link is done like so

Best Practice

index.html

 ... <!-- your html code -->
<script src = 'index.js'></script>

You must then have a javascript file in the path you have linked to

index.js

// your javascript code

You could also have the javascript code in your html. Note, however, that this approach is frowned upon because it makes your code hard to maintain as your code size grows in size.

Other way

index.html

 ... <!-- your html code -->
<script>
    // your javascript code
</script>

Hope that answers your question.