← Teacher guide

The real browser console

Students use console.log, warn and error to look inside a page, find elements with querySelectorAll and count them, read an error message for its name and line, and try the real browser Console.

The DOM and the browser console · Final challenge: a quiz page · about 45 minutes · Open the student lesson

Classroom resources

Load these into Waypoints Prepare: choose Load URL and paste the copied address, or download the file and import it. In Prepare, the worksheet's Teacher edition shows the answers; the student copy hides them.

Students will be able to

Timing

Predict and Run 10 min · Investigate 10 min · Modify 10 min · Make 15 min

Before class

Optional enrichment after the challenge. The playground's Console shows what console.log, warn and error write, and what the browser would say about an error, in plain words first. In the real browser, F12 (or right-click, Inspect) opens the DevTools; the Console tab is the same idea, with more tools. Check the school's browsers allow DevTools before the lesson: some managed devices block it, and the Make step then stays in the playground. Make a point of the safety rule: never paste code into the Console because someone told you to. Changes made in the real Console last until the page is reloaded and do not change the real site.

Running the lesson

  1. Predict: ask students to count console lines before running.
  2. Run: ask where the messages appear and where they do not.
  3. Investigate: for the bug, ask "what is the exact word it does not know?"
  4. Modify: ask what querySelectorAll gives that querySelector does not.
  5. Make: if allowed, demonstrate F12 on the class page together.

The hard part (on purpose)

Reading the error word for word: it names what it could not find. Ask "what is the exact word, and where did you make it?"

When students get stuck

What you will seeA question to askThe fix
Nothing appears in the Console.Was Run pressed, and is there a console.log line?Press Run and check the spelling of console.log.
X is not defined.Is X spelled the same as the name you made?Compare the two names letter by letter.
Cannot read properties of null.Did the selector find anything?Check the selector against the HTML.
F12 does nothing.Is DevTools blocked on this device?Use the playground Console instead; try right-click, Inspect.

Answers to the checks

  1. Predict: How many messages does the Console show?
    4. There are two console.log lines, one console.warn and one console.error. Each writes a message in the Console, in its own style.
  2. Which piece does what?: Match each piece with what it does.
    console.log("Heading:", heading.textContent); → D. writes a message with a label and the value; console.warn("Careful!"); → C. writes a warning in the Console; document.querySelectorAll("p").length → A. how many paragraphs are on the page; document.querySelector("p") → B. the first paragraph only
  3. Reading an error: The Console says "ReferenceError: cout is not defined" on line 6. What should you do first?
    Look at line 6 for a name that is not spelled the same as one you made
    The message names the word it does not know (cout) and the line. Compare it with the names you made: count is not cout.
  4. Spot the bug: The Console shows "cout is not defined". Click the line where the Console says it happened.
    line 6
    The variable is called count, but line 6 writes cout. JavaScript treats them as different names and cannot find the second.
  5. Where this leads: You have finished the course. What is the best way to get better at making pages that react?
    Make something you want, and use the Console to find out why it does not work yet
    Making pages you care about, with the Console open, is how developers learn. Errors are information, not failure.

Solutions

One possible solution for each task. Other correct programs are fine.

Modify › How many paragraphs?

<h1>Sports day</h1>
<p>Bring a hat.</p>
<p>Bring water.</p>
<script>
  const paragraphs = document.querySelectorAll("p");
  console.log("Paragraphs on the page:", paragraphs.length);
</script>

Modify › Each paragraph

<h1>Sports day</h1>
<p>Bring a hat.</p>
<p>Bring water.</p>
<script>
  const paragraphs = document.querySelectorAll("p");
  for (const p of paragraphs) {
    console.log(p.textContent);
  }
</script>

explore a page

<h1>Explore me</h1>
<p>One paragraph.</p>
<p class="note">Another, with a class.</p>
<script>
  console.log("Title:", document.querySelector("h1").textContent);
  console.log("Paragraphs:", document.querySelectorAll("p").length);
  console.log("Notes:", document.querySelectorAll(".note").length);
  console.log("The note says:", document.querySelector(".note").textContent);
</script>

Stretch and support

Stretch: Use the real Console on a favourite site to find a heading with document.querySelector("h1").textContent, and change it to see the page change (until reload).

Support: Give the loop and ask students only for the labels in the messages.

Exit ticket

What two things does an error message tell you?

The word it could not make sense of, and the line it happened on.

What each step is for

Where it leads

From here: students build their own page project, or move on to the Python web apps course.