← ACARA Digital Technologies Years 7–8

Commenting out code to find a bug

Putting // in front of a line makes JavaScript skip it, so you can switch code off to find a bug. Remove the // to switch it back on.

Understanding Commenting out code to find a bug in ACARA Digital Technologies Years 7–8

What it does: The // stops the line from running, so you can see if the bug disappears without deleting the code. Taking the // away switches the line back on, so the program works as it did before. Anything after // on a line is skipped. Putting // at the start switches off the whole line. Putting it at the end switches off nothing. If the problem goes away when a line is switched off, that line is a good suspect. Comment out one line at a time to isolate the bug. Deleting the // switches the code back on. Adding more slashes keeps it switched off.

Common mistakes: # is not a comment in JavaScript, so this line is a syntax error. To switch a line off you put // in front of it: // console.log(score * 2);. A single / does not start a comment, so JavaScript tries to read it as division and gives a syntax error. A comment needs two slashes: //.

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
You want JavaScript to skip this line while you hunt for a bug: console.log(total); Which version does that?
console.log(total); //// console.log(total);
Matches
/ console.log(total);
↓
Only one slash was typed. A comment needs two slashes //, so JavaScript sees a syntax error instead of a skipped line.
Applying
Meanings
Term
// console.log(total);
Meaning
Putting // in front of a line makes JavaScript skip it, so you can switch code off to find a bug. Remove the // to switch it back on.
Practice
let price = 10;
Testing
Gaps
console.log(total);
Check Answer
Spots
1# console.log(score * 2);
2let score = 10;

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

The 'Lion's Mane Jellyfish' badge — Set Operator, 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. You want JavaScript to skip this line while you hunt for a bug: console.log(total); Which version does that?
A. // console.log(total); ✓
B. console.log(total); //
Anything after // on a line is skipped. Putting // at the start switches off the whole line. Putting it at the end switches off nothing.
2. What happens when you comment out a line with //?
A. JavaScript skips the line but it stays in your file ✓
B. JavaScript deletes the line from your file
Commenting out only switches the line off. The code is still there, so you can switch it back on later.
3. You commented out a line and the bug disappeared. What does this suggest?
A. The bug is probably caused by that line ✓
B. The bug is definitely somewhere else
If the problem goes away when a line is switched off, that line is a good suspect. Comment out one line at a time to isolate the bug.
4. Which is the best way to find a bug by commenting out?
A. Comment out every line at once, then run the program
B. Comment out one line, run the program, then check the result ✓
Switching off one line at a time shows you which line causes the problem. Switching off everything tells you nothing.
5. You finished testing. How do you restore the line // let score = 10;?
A. Add another // so it reads //// let score = 10;
B. Remove the // so it reads let score = 10; ✓
Deleting the // switches the code back on. Adding more slashes keeps it switched off.

Match the Pairs

// console.log(total); → This line is commented out, so JavaScript skips it and does not print total.
/* let bonus = 5; score += bonus; */ → A block comment that switches off several lines at once so none of them run.
To test if a line is causing a bug, switch it off by putting // in front of it. → // score = score * 2;
After you finish testing, restore the code by removing the // so the line runs again. → console.log(total);
/ console.log(total); → Only one slash was typed. A comment needs two slashes //, so JavaScript sees a syntax error instead of a skipped line.
/* let bonus = 5; score += bonus; → The block comment is never closed. It needs */ at the end, or the rest of the program is swallowed and an error appears.

Key Term

// console.log(total);
Putting // in front of a line makes JavaScript skip it, so you can switch code off to find a bug. Remove the // to switch it back on.

Fill in the Blanks

1. // console.log(total);
Hint: Type the two slash symbols that switch this line off so JavaScript skips it.
2. let total = 0; // total = total - 5; console.log(total);
Hint: Type the symbol that switches off the suspect line so you can see if it causes the bug.
3. /* console.log(a); console.log(b); */
Hint: Type the symbol that opens a block comment and switches off several lines at once.
4. /* total = total - 5; */ console.log(total);
Hint: Type the symbol that closes a block comment.
5. let total = 4 * 5; // Line below is switched back on console.log(total);
Hint: The // was removed to restore the line. Type the function name that prints to the console.

Spot the Error

Question 1
A # console.log(score * 2);
B let score = 10;
C score = score + 5;
D console.log(score);
Line A — # is not a comment in JavaScript, so this line is a syntax error. To switch a line off you put // in front of it: // console.log(score * 2);
Question 2
A let total = 0;
B total = total + 5;
C / total = total * 2;
D console.log(total);
Line C — A single / does not start a comment, so JavaScript tries to read it as division and gives a syntax error. A comment needs two slashes: //
Question 3
A */
B console.log("one");
C console.log("two");
D */
E console.log("three");
Line A — A block comment must start with /* and end with */. Starting with */ is a syntax error, so the first line should be /*
Question 4
A let total = 0;
B for (let i = 1; i < 4; i++) {
C total += i;
D // }
E console.log(total);
Line D — Commenting out the closing brace leaves the loop open and causes a syntax error. Only comment out whole statements you want to test, and keep paired symbols like { } together. The line should be }
Question 5
A let a = 3;
B console.log(a + b); //
C let b = 4;
D console.log(a * b);
Line B — The // only switches off the text after it, so console.log(a + b); still runs and causes an error because b is not ready yet. Put // at the start of the line: // console.log(a + b);

Student with a class code? Join your class →