Himanshu SharmaSoftware engineer
Let’s talk

JavaScript · Lesson 2 of 58

Running JavaScript: script tags and the console

Run JavaScript in a page with script tags, load an external file with defer, and use the browser console and console.log to see what your code does.

11 minGetting startedUpdated 14 November 2026

Awaiting technical review. This lesson is readable but not yet listed in search engines.

By the end of this lessonAdd JavaScript to an HTML page in the right place, open the browser console, and use console.log to inspect values and find errors.

By the end of this lesson you can put JavaScript into a page, load it from a separate file, and use the browser console to see what your code is doing. This is the workflow you will use for the rest of the course.

The script tag

JavaScript goes into a page with the <script> element. There are two ways to use it.

Inline, with the code written between the tags:

html
<script>
  console.log("This runs when the browser reaches this tag.");
</script>

External, with the code in its own file and a src attribute that points to it:

html
<script src="app.js"></script>

For anything beyond a few lines, an external file is better. The code gets its own place, the browser can cache the file, and several pages can share it. Inline scripts are handy for small examples like the ones in this course.

Order matters

The browser reads an HTML page from top to bottom. When it meets a normal script, it stops, runs the script, and only then carries on reading. So a script can only see the elements that came before it.

Run this example. The script sits above the paragraph it tries to change.

html
<div id="report"></div>

<script>
  const report = document.getElementById("report");
  const first = document.getElementById("late");
  report.textContent = "Found it? " + (first !== null);
</script>

<p id="late">I am below the script.</p>

The answer is false, because document.getElementById returns null when nothing matches yet. Now move the whole <script> block to the bottom, after the paragraph, and run it again. It says true. The fix is always the same idea: let the page exist before the script looks for it.

Where to put scripts

You have three reliable options.

  1. At the end of the body, just before </body>. By then everything above has been read.
  2. In the head with defer, for an external file: <script src="app.js" defer></script>. The browser downloads the file while it reads the page, then runs it once the HTML has been fully parsed. Several deferred scripts run in the order they are written.
  3. As a module, with <script type="module" src="app.js"></script>. Modules are deferred automatically. You will meet them near the end of the course.

Note that defer only works with an src. It has no effect on inline code, so keep inline scripts at the bottom of the body.

You may also see async. It runs the script the moment it has downloaded, in no guaranteed order. That is right for independent things like analytics, and wrong for scripts that depend on each other.

The console

The browser's developer tools include a console: a panel where your code can print messages and where the browser reports errors. To open it:

  • Press F12, or Ctrl+Shift+J on Windows and Linux, or Cmd+Option+J on a Mac in Chrome.
  • Or right-click the page, choose Inspect, and pick the Console tab.

The exact shortcut can differ between browsers, but the right-click route works in all of them.

You can type JavaScript directly into the console and press Enter to run it. It is a scratchpad for trying an idea in seconds.

console.log

console.log prints whatever you give it. Separate several values with commas and they appear on one line.

javascript
console.log("Hello", 42, true);
console.log("Total:", 3 * 4);
console.log({ a: 1, b: [1, 2] });

The console shows:

result
Hello 42 true
Total: 12
{ a: 1, b: [ 1, 2 ] }

The last line is how Node.js formats an object. Browsers print it in their own, expandable way, but it holds the same information. Passing several values separated by commas is better than gluing them together with +, because the console keeps each value's real type and lets you expand objects.

Other methods you will meet:

  • console.error("message") prints in red, for problems.
  • console.warn("message") prints as a warning.
  • console.table(array) shows an array of objects as a table.

The in-page editors in this course do not display the console, so those examples write their results into the page. In your own files, use console.log freely.

Reading errors

When something goes wrong, the console shows a red message. For example, if you misspell a variable, you get something like ReferenceError: mesage is not defined, followed by the file name and a line number. Click it and the browser takes you to the line. Read the first line of the message carefully. It usually names the exact problem. You will meet a full set of debugging tools in a later lesson, but the console message is where every debugging session starts.

Putting it together

Here is a complete small page with an external script. Save these as two files in the same folder, and open index.html.

html
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>My first script</title>
  <script src="app.js" defer></script>
</head>
<body>
  <h1 id="title">Hello</h1>
</body>
</html>
javascript
// app.js
const title = document.getElementById("title");
console.log("Found the heading:", title.textContent);
title.textContent = "Hello from app.js";

Because of defer, the heading exists by the time app.js runs, even though the script tag is in the head.

Common mistakes

  • Putting the script before the element it needs. The most common beginner problem. Use defer or move the script to the end of the body.
  • Writing code inside a script tag that also has a src. When src is present, the code between the tags is ignored. Use one or the other.
  • A wrong file path. If app.js is in a js folder, the tag must say src="js/app.js". A missing file shows a 404 in the console's Network panel, and nothing runs.
  • Forgetting to save or refresh. You edit the file, but the browser still shows the old version. Save, then reload. A hard reload (Ctrl+Shift+R) skips the cache.
  • Ignoring the console. A page that "does nothing" has usually printed an error you did not look at.

Exercise

  1. Create index.html and app.js as above and open the page.
  2. Open the console and check that your console.log line appears.
  3. Add a second console.log that prints the number of characters in the heading. Use title.textContent.length.
  4. Now misspell title as titel on purpose, reload, and read the red error. Fix it again.

When you are comfortable, continue to Variables: let, const and var.

Key points

  • A script tag holds JavaScript inline, or points to an external file with its src attribute.
  • Scripts run when the browser reaches them, so a script placed before an element cannot find that element.
  • Adding defer to an external script waits until the page is parsed, and keeps the scripts in order.
  • console.log prints values to the browser console, which you open from the developer tools.
  • Red error messages in the console name the file and line, and are the first place to look when something breaks.

Check yourself

1. A script in the head tries to find a button that is later in the body, and finds nothing. Why?
2. What does the defer attribute on an external script do?
3. Where does console.log print its output in a browser?
4. Does defer have any effect on an inline script that has no src attribute?

Free tools for this lesson