← ACARA Digital Technologies Years 7–8

Debugging with console.log()

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.

Understanding Debugging with console.log() in ACARA Digital Technologies Years 7–8

What it does: A console.log inside the loop body runs on every repeat, so you see each value of i and can spot where it first goes wrong. Commenting out (or deleting) the debug line stops it printing. The program still works and the console only shows the real output. A label tells you which variable the number belongs to, which matters when you print several values. Logging the parameters shows what the function actually received. You can also log the result before returning it. The label and the variable are separated by a comma inside the brackets. console.log only displays a value. It does not change the variable, which makes it safe for checking your code.

Common mistakes: 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. =+ 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.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 624–634) and word search (Modules 611–634).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
Your program prints the wrong total. What is a good first debugging step?
Add console.log("total:", total) to see the value as the program runsDelete the whole program and start again
Matches
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);
Applying
Meanings
Term
console.log("total:", total)
Meaning
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.
Practice
let total = 0;
Testing
Gaps
console.("total:", total);
Check Answer
Spots
1let total = 0;
2let total = total + 5;

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Ermine' badge — Tundra Streak, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

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
console.log lines let you watch a variable change so you can find where it first goes wrong.
2. Which debug message is easier to understand in the console?
A. console.log("score:", score); ✓
B. console.log(score);
A label tells you which variable the number belongs to, which matters when you print several values.
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
Logging inside the loop shows the value on every pass, so you can spot the pass where it first goes wrong.
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); ✓
Logging the parameters shows what the function actually received. You can also log the result before returning it.
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 ✓
The first unexpected value points to the line just before it, which is where you should look.

Match the Pairs

console.log("total:", total); → Prints a labelled value of total so you can check what it holds at that point in the program
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
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); }
Once the bug is fixed, remove or comment out the debug line so the console stays clean → // console.log("score:", score);
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);
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);

Key Term

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.

Fill in the Blanks

1. let total = 0; total = total + 5; console.log("total:", total);
Hint: Type the console method that prints a labelled value so you can check it.
2. for (let i = 1; i <= 3; i++) { console.log("i:", i); }
Hint: Type the loop counter variable so each pass through the loop shows its value.
3. function double(n) { return n * 2; } const result = double(4); console.log("result:", result);
Hint: Type the variable that holds the value the function gave back.
4. function greet(name) { console.log("name:", name); }
Hint: Type the parameter name so the debug line shows what the function received.
5. let score = 0; score += 5; // console.log("score:", score);
Hint: Type the symbols that switch off a debug line once the bug is fixed, without deleting it.

Spot the Error

Question 1
A let total = 0;
B let total = total + 5;
C console.log("total:", total);
Line 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.
Question 2
A let sum = 0;
B for (let i = 1; i <= 3; i++) {
C sum =+ i;
D console.log("sum:", sum);
E }
Line 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.
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 }
Line 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.
Question 4
A let score = 10;
B score = score + 5;
C console.log("score:", score;
Line C — The closing bracket is missing. It should be console.log("score:", score); so the debug line runs and shows the value of score.
Question 5
A let price = 20;
B let qty = 3;
C let cost = price * qty;
D console.log("cost:", Cost);
Line D — JavaScript is case sensitive. The variable is cost, not Cost, so this causes a ReferenceError. The debug line should log cost.

Student with a class code? Join your class →