JavaScript JavaScript Basics Introducing JavaScript The Console Challenge Answer

Jesse Vorvick
Jesse Vorvick
Full Stack JavaScript Techdegree Student 3,362 Points

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">
  <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>
  <div class="container">
  <h1>Can you get the JavaScript to work?</h1>
  <script src="scripts.js"></script>
  <script>console.log("End program")</script>

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
Treehouse Moderator 28,849 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.

Jesse Vorvick
Jesse Vorvick
Full Stack JavaScript Techdegree Student 3,362 Points

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
Treehouse Moderator 28,849 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.