JavaScript JavaScript Basics Introducing JavaScript The Console Challenge Answer

Tracy Nickel
Tracy Nickel
4,230 Points

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

One Weru
One Weru
47,621 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.