Debugging with console.log()

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Debugging with console.log()

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Your program prints the wrong total. What is a good first debugging step?
A Add console.log("total:", total) to see the value as the program runs
B Delete the whole program and start again
2. Which debug message is easier to understand in the console?
A console.log("score:", score);
B console.log(score);
3. You want to see how total changes on every repeat of a loop. Where should the debug line go?
A Inside the loop body, after total is updated
B Only after the loop has finished
4. A function add(a, b) gives the wrong answer. Which debug line helps most?
A console.log("add is a function");
B console.log("a:", a, "b:", b);
5. Your debug output shows count: 1, count: 2, count: 4 when you expected 3. What does this tell you?
A The bug must be somewhere after the last log line
B The value first goes wrong between the second and third logs

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1console.log("total:", total); AA comma is missing between the label and the variable, so this is a syntax error and nothing is logged. It should be console.log("total:", total);
2console.log("n =", n, "result =", result); Bfor (let i = 0; i < 5; i++) { console.log("i:", i); }
3Log the counter inside a loop to see how its value changes on every repeat CThe variable total is inside the quotes, so only the word is printed, not the value. It should be console.log("total:", total);
4Once the bug is fixed, remove or comment out the debug line so the console stays clean D// console.log("score:", score);
5console.log("total:" total); ELogs a function's parameter and its result together so you can see what went in and what came out
6console.log("total:"); FPrints a labelled value of total so you can check what it holds at that point in the program

Definitions

console.log("total:", total)

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
let total = 0; total = total + 5; console.___("total:", total);
2.
for (let i = 1; i <= 3; i++) { console.log("i:", _); }
3.
function double(n) { return n * 2; } const result = double(4); console.log("result:", ______);
4.
function greet(name) { console.log("name:", ____); }
5.
let score = 0; score += 5; __ console.log("score:", score);

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A let total = 0;
B let total = total + 5;
C console.log("total:", total);
Question 2
A let sum = 0;
B for (let i = 1; i <= 3; i++) {
C sum =+ i;
D console.log("sum:", sum);
E }
Question 3
A function add(a, b) {
B console.log("a:", a);
C let sum = a + b;
D console.log("sum:" sum);
E return sum;
F }
Question 4
A let score = 10;
B score = score + 5;
C console.log("score:", score;
Question 5
A let price = 20;
B let qty = 3;
C let cost = price * qty;
D console.log("cost:", Cost);

Answer Key

Debugging with console.log()
Section A: Quick Questions
Q1
A — Add console.log("total:", total) to see the value as the program runs
Q2
A — console.log("score:", score);
Q3
A — Inside the loop body, after total is updated
Q4
B — console.log("a:", a, "b:", b);
Q5
B — The value first goes wrong between the second and third logs
Section B: Match the Pairs
Q1
console.log("total:", total); → Prints a labelled value of total so you can check what it holds at that point in the program
Q2
console.log("n =", n, "result =", result); → Logs a function's parameter and its result together so you can see what went in and what came out
Q3
Log the counter inside a loop to see how its value changes on every repeat → for (let i = 0; i < 5; i++) { console.log("i:", i); }
Q4
Once the bug is fixed, remove or comment out the debug line so the console stays clean → // console.log("score:", score);
Q5
console.log("total:" total); → A comma is missing between the label and the variable, so this is a syntax error and nothing is logged. It should be console.log("total:", total);
Q6
console.log("total:"); → The variable total is inside the quotes, so only the word is printed, not the value. It should be console.log("total:", total);
Section C: Definitions
Q1
console.log("total:", total) — Prints a labelled variable value while the program runs, so you can spot the line where the value first goes wrong. Remove these debug lines once the bug is fixed.
Section D: Fill in the Blanks
Q1
let total = 0; total = total + 5; console.log("total:", total);
Q2
for (let i = 1; i <= 3; i++) { console.log("i:", i); }
Q3
function double(n) { return n * 2; } const result = double(4); console.log("result:", result);
Q4
function greet(name) { console.log("name:", name); }
Q5
let score = 0; score += 5; // console.log("score:", score);
Section E: Spot the Error
Q1
B — total is already declared on line 1, so let cannot declare it again. Line 2 should reassign it with total = total + 5; so the console.log on line 3 can show the new value.
Q2
C — =+ sets sum to positive i instead of adding to it. The logged sum would show 1, 2, 3, not 1, 3, 6. It should be sum += i; and the log inside the loop is how you spot where the value goes wrong.
Q3
D — The comma between the label and the variable is missing. It should be console.log("sum:", sum); so the function's result is logged with its label.
Q4
C — The closing bracket is missing. It should be console.log("score:", score); so the debug line runs and shows the value of score.
Q5
D — JavaScript is case sensitive. The variable is cost, not Cost, so this causes a ReferenceError. The debug line should log cost.