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: Inline vs Imported JavaScript question

Watching this video, I was happy to see that Dave debugged the program mostly the same way I did in response to the challenge in the previous video. However, there was one difference.

Dave typed the console.log functions into script.js, whereas I had typed it into index.html, doing my best to keep them at the beginning and ends of the program, like so:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <script>console.log("Begin program")</script>
  <link rel="stylesheet" href="css/main.css">
  <title>What's wrong with this script?</title>
 </head>
<body>
  <div class="container">
  <h1>Can you get the JavaScript to work?</h1>
  <script src="scripts.js"></script>
  </div>
  <script>console.log("End program")</script>
</body>
</html>

I got the same result as Dave in the preview's console. My question is, is this just as valid, or is it a bad habit for reasons that will be revealed later on as I gain more experience?

Also, were my inline console.log functions truly placed in the "beginning" and "end" of the program through my method?

2 Answers

Tim Knight
Tim Knight
28,888 Points

Hi Jesse,

It's absolutely just as valid when you want to see when the script started and ended like you did. However it's often best to run it in the script itself so you don't have to add additional script calls. Also adding it in the script would let you add console.log commands in the middle of functions to see that specific functionality is running correctly.

Thanks Tim!

One more thing: How do I show code in forum posts without it congealing into one paragraph? I copied and pasted my code as it was in Workspace, but when I submitted my question it all became one line.

Tim Knight
Tim Knight
28,888 Points

Jesse you'll see a link for "Markdown Cheatsheet" are the text input for your comments and posts. Just click that and it'll give you details on how to format your posts and code.

Thank you!