JavaScript JavaScript Basics Introducing JavaScript The Console Challenge Answer

Console.log Messages

In the video, Dave put the console.log messages in the writes.js file. I had put them in the index.html file - I put the Start Program function after the opening body tag, and the End Program function just before the closing body tag. Here's the markdown:

  <script> console.log("Start program");</script>
  <div class="container">
  <h1>Can you get the JavaScript to work?</h1>
  <script src="scripts.js"></script>
  <script> console.log("End program");</script>

My way worked, but is the writes.js placement the "best practice" way to do it?

2 Answers

Steve's right.

In this example, you can really get away with either, but as you get into more sophisticated code that you need to debug and refactor, you are going to want it as organized as possible.

I would say any time you have an opportunity to keep code or a stylesheet in an external script that you include in your HTML page - the better. It allows for proper separation and cleaner code. To that degree, I would say absolutely it is best practice. It won't always be the case, but in most cases it will be.

Brandon Barrette
Brandon Barrette
20,485 Points

Moved this to an answer instead of a comment because, well, it's a great answer! Thanks!

Thank you so much, Steve! I really appreciate it :)