TalentJR

🟨 JavaScript tutorial for beginners · Class 8 · Lesson 17 of 18 · Free

Debugging with Console – Find and Fix Bugs Like a Pro

⏱ About 35 minutes · ▶ run real JavaScript here — nothing to install · no sign-in

In this lesson you will

  • Tell apart syntax errors, runtime errors and logic bugs
  • Read a JavaScript error message and find the line it points to
  • Use console.log, console.table, console.count and breakpoints to find bugs

Your marks calculator says your average is 193.67 out of 100. That cannot be right! The code runs, but the answer is wrong. Every programmer meets bugs every day — even experts. In this lesson, you become a bug detective and learn the tools to catch them fast.

What is a bug in JavaScript, and what kinds are there?

A **bug** is a mistake in code. There are three kinds. A **syntax error** is broken grammar, like a missing bracket: nothing runs at all. A **runtime error** happens while running, like using a variable that does not exist: the program stops at that line.

The third kind is the sneakiest. A **logic bug** gives no error at all. The program runs happily and gives a wrong answer.

This code has a logic bug. The average of 3 marks should be between 0 and 100. Run it and look at the animation: which step goes wrong?

🎬 How JavaScript thinks — step 1 of 8JavaScript is about to run line 1
▶const maths = 78;
2const science = 85;
3const english = 92;
4
5const average = maths + science + english / 3;
6console.log("Average:", average);
7
8// the fix: brackets first, like BODMAS
9const fixed = (maths + science + english) / 3;
10console.log("Fixed average:", fixed);
📦 Variables (memory boxes)

No variables yet.

🖥️ Output
▌
✏️ Change this code and run it yourself
🟨 JavaScript — you can edit this code
Console output
Average: 193.66666666666666
Fixed average: 85

💡 JavaScript follows BODMAS: / is done before +. So only english was divided by 3.

How do I use console.log to find a bug in JavaScript?

The number one trick: **print the values while the code runs**. Put a `console.log` inside the loop and look at every step.

Always add a label, like `console.log("i:", i, "total:", total)`. Plain numbers with no label are confusing.

This loop adds cricket runs, but the total comes out as NaN. The log shows that the last round reads `runs[4]`, which is undefined. The loop goes one step too far: `<=` should be `<`.

🎬 How JavaScript thinks — step 1 of 19JavaScript is about to run line 1
▶const runs = [12, 40, 7, 33];
2let total = 0;
3
4for (let i = 0; i <= runs.length; i++) {
5 total = total + runs[i];
6 console.log("i:", i, "runs[i]:", runs[i], "total:", total);
7}
8console.log("Team total:", total);
📦 Variables (memory boxes)

No variables yet.

🖥️ Output
▌
✏️ Change this code and run it yourself
🟨 JavaScript — you can edit this code
Console output
i: 0 runs[i]: 12 total: 12
i: 1 runs[i]: 40 total: 52
i: 2 runs[i]: 7 total: 59
i: 3 runs[i]: 33 total: 92
i: 4 runs[i]: undefined total: NaN
Team total: NaN

💡 Try it: change <= to < and run again. The total becomes 92.

How do I read a JavaScript error message?

A runtime error message has three useful parts: the **type** (like TypeError), the **message** (what went wrong) and the **line number**.

**ReferenceError** means a name does not exist — often a spelling mistake. **TypeError** means you used a value the wrong way, like reading a property of `undefined`.

Here we catch the errors with `try…catch` so the program can keep running and show them to you. You will learn try…catch properly in Class 9 Lesson 5.

🟨 JavaScript — you can edit this code
Console output
ReferenceError → studnet is not defined
TypeError → Cannot read properties of undefined (reading 'maths')

💡 In your own programs, the error shows in red with "Error on line …". Go to that line first!

What are console.warn, console.error, console.table and console.count?

`console` has more tools than `log`. `console.warn` prints a yellow warning. `console.error` prints a red error. Use them so important messages stand out.

`console.table` shows an array of objects as a neat table — great for checking data. `console.count("label")` counts how many times a line ran.

`console.assert(check, message)` prints an error only when the check is false. It is a quick "this must be true" alarm.

🟨 JavaScript — you can edit this code
Console output
│ (index) │ name │ marks │
│ 0 │ "Asha" │ 91 │
│ 1 │ "Ravi" │ 34 │
│ 2 │ "Meera" │ 78 │
checked: 1
checked: 2
Ravi needs help: 34
checked: 3
Assertion failed: Expected 4 students!
Example error: roll number missing

How do I debug a button that does not work?

Page bugs need the same detective work. First question: **does the click even reach my code?** Put a `console.log` as the first line of the handler.

Second question: **what type is the value?** `textContent` is always a string. "1" + 1 gives "11", not 2! `typeof` tells you the truth.

This counter has the fix already: `Number(...)`. Click it and watch the log. Then remove `Number( )` and see the bug.

🟨 JavaScript — you can edit this code
📄 HTML of the page (tap to see / edit)
🖥️ Your web page — try clicking it
Console output
Start type: string

How do I use breakpoints and the debugger in browser DevTools?

Too many console.logs get messy. A **breakpoint** pauses the program on a line, so you can look at every variable at that moment.

In Chrome: press **F12**, open the **Sources** tab, and click a line number. Or write `debugger;` in your code. When DevTools is open, the program stops there. Then press the step button to run one line at a time.

When DevTools is closed, `debugger;` does nothing, so this code runs normally here.

🟨 JavaScript — you can edit this code
Console output
Canteen bill: ₹225

💡 Remember to delete debugger; lines before you share your project.

🤔 Predict the output

Think like JavaScript: read the code and choose what it prints. You get the reason straight away.

Question 1. What does this print?

const marks = [80, 90];
let total = 0;
for (let i = 0; i <= marks.length; i++) {
  total = total + marks[i];
}
console.log(total);

Question 2. What does this print?

console.log("5" + 3, "5" - 3);

Question 3. What does this print?

for (let i = 0; i < 2; i++) {
  console.count("click");
}

🧩 Your challenge

This **pocket money** program has 2 bugs. Rahul spent ₹50, ₹120 and ₹30 out of ₹500. Add console.log lines inside the loop to find the bugs, then fix them. Expected output after fixing: ``` Spent ₹200, left ₹300 ```

Expected output in the console:
Spent ₹200, left ₹300
🟨 JavaScript — you can edit this code

⚠️ Common mistakes (and how to fix them)

❌ Wrong

let total = 250;
total = total + 50;
console.log(totl);

ReferenceError: totl is not defined

✅ Right

let total = 250;
total = total + 50;
console.log(total);

A ReferenceError usually means a spelling mistake. Compare the name in the error with the name you declared.

❌ Wrong

const score = 47;
console.log("score");

No error, but it prints the word score, not the value 47.

✅ Right

const score = 47;
console.log("score:", score);

Text in quotes is printed as it is. To see the value, write the variable name without quotes — and a label in quotes before it.

❌ Wrong

const student = { name: "Riya", marks: { maths: 95 } };
console.log(student.mark.maths);

TypeError: Cannot read properties of undefined (reading 'maths')

✅ Right

const student = { name: "Riya", marks: { maths: 95 } };
console.log(student.marks.maths);

student.mark does not exist, so it is undefined, and you cannot read .maths of undefined. Print the object to check the exact key names.

📌 Remember

  • Three kinds of bugs: syntax errors, runtime errors and logic bugs (no error, wrong answer).
  • Read the error type, message and line number — then go to that line.
  • console.log with labels inside loops shows what really happens.
  • console.table, warn, error, count and assert make debugging easier.
  • Breakpoints or debugger; pause the code so you can check every variable.

Finished the quiz and the challenge?

Questions students ask

What is debugging in JavaScript?

Debugging means finding and fixing mistakes in your code. You look at error messages, print values with console.log, or pause the code with breakpoints until you understand what goes wrong.

How do I open the console in Chrome?

Press F12 (or Ctrl + Shift + J on Windows, Cmd + Option + J on Mac) and click the Console tab. Errors from your page show there in red.

What does "Cannot read properties of undefined" mean?

You tried to read something like x.name, but x is undefined. Usually a key is misspelt or an array index is too big. Print x just before that line to check.

Is it bad to get many errors?

Not at all. Every programmer gets errors all day. An error message is a clue that tells you where to look. Getting good at reading them is a big skill.