Skip to content
beginner

Debug JavaScript with Browser DevTools Breakpoints

That is the worst kind of bug for a beginner, because the console gives you nothing to read. So you start adding console.log() lines, reloading, guessing,…

Published 2026-10-03Updated 2026-10-049 min read
Crop anonymous male working on computer and typing on backlit keyboard placed near contemporary laptop on stand
Crop anonymous male working on computer and typing on backlit keyboard placed near contemporary laptop on stand. Photo by Anete Lusina on Pexels.

Your script runs. No red errors. And the total is still wrong.

That is the worst kind of bug for a beginner, because the console gives you nothing to read. So you start adding console.log() lines, reloading, guessing, and adding more. Ten minutes later you have six log statements and no answer.

A breakpoint ends the guessing. It freezes your program mid-run and lets you look at every value inside it — not just the ones you thought to print. Learning to debug JavaScript with breakpoints is the moment debugging stops being a guessing game and becomes an investigation.

Why a Breakpoint Beats Another console.log

A breakpoint is a marker you place on a line of code. It tells the browser: stop right before this line runs, and wait for me.

That pause is the whole point. When the program is frozen, you can see the real state of everything — every variable in the current function, the chain of function calls that got you here, and the exact line about to execute.

Compare that to console.log(). Logging shows you only the values you remembered to print, at the moments you decided to print them. If the bug lives in a variable you never thought about, your logs will never mention it. A paused program shows you everything at once.

There is a second advantage that matters more than beginners expect: you do not need to know where the bug is. With logging, you have to guess a location, insert a line, and reload. With a breakpoint, you pause near the suspect area and walk forward one line at a time until a value goes wrong. The bug reveals its own location.

You already know how to run a script and read console output. This is the next step for when output alone is not enough.

Open DevTools and Find Your File

Open your page in the browser, then open DevTools:

  • Press F12, or right-click anywhere on the page and choose Inspect.
  • In Chrome or Edge, click the Sources tab. In Firefox, click the Debugger tab. They do the same job under different names.

You will see three areas:

  1. The file tree on the left — every file the page loaded.
  2. The code editor in the middle — the contents of whichever file you select.
  3. The debugger panel on the right — Breakpoints, Scope, Call Stack, and Watch.

Click your script file in the tree. If your JavaScript is written inline inside the HTML, it still appears — look for the page file itself and select it.

Note: If the debugger panel is missing, your DevTools window may be too narrow. Widen it, or look for the collapsible sections at the bottom of the panel.

Set a Breakpoint and Pause the Program

Here is a small script with a bug in it. Save it as app.js and load it from an HTML page:

function addNumbers(a, b) {
  const total = a - b;
  return total;
}

const result = addNumbers(5, 3);
console.log("Result:", result);
Result: 2

The script runs cleanly. It also prints 2 when you expected 8. Nothing crashed, so the console has no error to show you.

Now find the line const total = a - b; in the Sources panel and click the line number in the gutter to the left of the code. A blue or highlighted marker appears. That is your breakpoint.

Reload the page with Ctrl+R (Windows/Linux) or Cmd+R (Mac).

The page freezes. The line you marked is highlighted, and the right-hand panel fills with information.

Common mistake: Execution stops before the marked line runs, not after. Beginners stare at the highlighted line assuming it already executed. It has not. The values you see are the state just before that line does its work.

Knowledge check

Check your understanding

Answer this question before you continue.

When the browser pauses at a breakpoint set on `const total = a - b;`, what is true about that line?
Misconception Check

Focus: Explain when execution pauses relative to a marked line.

Read the Scope Panel to Inspect Variables

With the program paused, look at the Scope panel on the right. This is where you inspect variables in browser DevTools, and it is where most beginner bugs get caught.

Scope is organized into sections:

  • Local — variables inside the function you are currently paused in. This is where you should look first.
  • Closure — variables from an outer function that this function can still see.
  • Global — variables available everywhere on the page.

In our example, Local shows a and b. Expand it and you will see a: 5 and b: 3. Those are correct. The function received exactly what you passed in.

So the inputs are fine. That narrows the search to one line.

You can also use Watch to pin a specific expression. Click the + in the Watch panel and type a - b. The debugger evaluates it and keeps it updated as you step. This is useful when you care about one calculation and do not want to scan the whole scope list every time.

Knowledge check

Check your understanding

Answer this question before you continue.

While paused inside `addNumbers(a, b)`, which Scope section should you check first to verify the function's arguments?
Single Choice

Focus: Use the Scope panel to inspect function-local values while execution is paused.

Step Through the Function Line by Line

A flow shows a breakpoint pausing before a line, stepping revealing a wrong value, and the code being fixed and verified, with a loop back to inspection if the result is still wrong.
Step through execution to locate the line where a value first goes wrong, then verify the fix.

Stepping means running your code one line at a time so you can watch values change. The controls sit at the top of the debugger panel:

ControlWhat it doesUse it when
Step overRuns the current line, moves to the next line in the same functionYour default move — use it most of the time
Step intoFollows a function call down into that function's bodyYou suspect the bug is inside a function being called
Step outFinishes the current function and returns to whoever called itYou stepped in by accident, or you trust the rest of this function
ResumeContinues running until the next breakpoint or the endYou are done inspecting and want the page to finish

Start with Step over. Press it once. The line const total = a - b; executes, and the Scope panel updates. Now total appears in Local with a value of 2.

There it is. The function received 5 and 3, and produced 2. The inputs were right; the calculation was wrong. You have landed on the exact line where the value diverges from what you expected.

Notice what just happened. You did not guess. You watched the value appear, and the wrong number pointed at its own line.

Tip: If you step too far and lose your place, press Resume and reload the page. The breakpoint is still set, so you will pause in the same spot again.

Knowledge check

Check your understanding

Answer this question before you continue.

Execution is paused just before `const total = a - b;`, with `a` equal to 5 and `b` equal to 3. After one Step over, what value appears for `total` in Local?
Output Prediction

Focus: Predict the local value created by stepping over the faulty subtraction line.

Fix the Line and Verify the Fix

Go back to your editor and change the operator:

function addNumbers(a, b) {
  const total = a + b;
  return total;
}

const result = addNumbers(5, 3);
console.log("Result:", result);
Result: 8

Before you reload, remove or disable the breakpoint. Click the line number again to toggle it off, or uncheck it in the Breakpoints panel. If you leave it on, the page will pause every single time it loads, which looks exactly like a frozen website.

Reload and check the page. The output should now read 8.

If it is still wrong, set the breakpoint again and repeat the loop. That loop — pause, step, inspect, fix, verify — is the actual skill. The first guess is not.

Knowledge check

Check your understanding

Answer this question before you continue.

The debugger confirms `a` is 5 and `b` is 3, but the function named `addNumbers` produces 2. Which change fixes the demonstrated bug?
Debugging

Focus: Correct a faulty arithmetic line after inspecting the inputs and result.

Common Beginner Mistakes at the Breakpoint

Most people who decide breakpoints "do not work" hit one of these:

  • Setting a breakpoint but never reloading. The script already ran. It will not run again until you reload the page or trigger the code again.
  • Expecting the marked line to have executed. It pauses before that line. Always.
  • Looking in the wrong scope. A variable you expect in Local may live in Closure or Global. Expand all three before concluding a value is missing.
  • Leaving breakpoints enabled. Every page load will pause. Disable them when you are done.
  • Forgetting the debugger statement. You can also write debugger; directly in your code to pause there. It only works while DevTools is open, and it lives in your source — so remember to delete it before you ship.

When to Use Breakpoints and When Not To

Breakpoints are not the answer to every problem. Here is the rule I use:

SituationBetter tool
Logic runs but produces the wrong valueBreakpoint
You cannot tell which line is responsibleBreakpoint
The bug only appears on one pass of a loop or one eventBreakpoint
You need one value at one moment in a short scriptconsole.log()
A file fails to load, or scripts run in the wrong orderCheck the Network and Console panels
A selector never matches any elementInspect the DOM, not the execution

The pattern: breakpoints are for execution problems — code that runs but behaves wrongly. Setup and structure problems need different tools, and pausing execution will not help you find a missing file.

Practice: Break a Function on Purpose

The fastest way to make this stick is to create the bug yourself.

  1. Write a function that takes two numbers and returns their sum.
  2. Deliberately change one operator — swap + for -, or change an operand.
  3. Set a breakpoint on the first line inside the function, reload, and step through until the wrong value appears.
  4. Write down the value you expected and the value you observed at each step.
  5. Fix the line, remove the breakpoint, and confirm the output matches your expectation.

Do it twice with two different bugs. The second time, you will notice you are not nervous anymore — you are just looking.

Where to Go Next

When a script runs but produces the wrong result, stop guessing and pause it. That is the habit worth keeping.

The next natural step is applying the same stepping skills to a loop or an event handler, where the bug only shows up on one specific pass. That is where stepping stops feeling like a trick and starts feeling like a superpower: you can watch iteration five of a loop the same way you just watched a single line.

Knowledge check

Final check

Finish the article by checking the ideas you just learned.

At the breakpoint, you have confirmed the function received the expected inputs. What is the most useful next action for locating the faulty calculation?
Question 1 of 2Single Choice

Focus: Choose a useful next debugging action after confirming the function inputs are correct.

A JavaScript file fails to load. Based on the article's tool-selection guidance, what should you check rather than relying on a breakpoint?
Question 2 of 2Misconception Check

Focus: Distinguish execution bugs suited to breakpoints from file-loading problems that need other panels.

References

  1. Debug JavaScript  |  Chrome DevTools  |  Chrome for Developersdeveloper.chrome.com
  2. Debuggen und Fehlerbehandlung in JavaScript - Webentwicklung lernen | MDNdeveloper.mozilla.org
Practical resource

Want a more structured JavaScript path?

Use the JavaScript for AI Applications Starter Pack to turn individual tutorials into a focused path from language fundamentals to interactive AI applications.

View the bundle
Coming soon

JavaScript for AI Applications Starter Pack

Build the JavaScript foundation behind modern interactive applications. The JavaScript for AI Applications Starter Pack takes you from core language concepts through functions, browser events, forms, DOM updates, application state, debugging, and practical projects—so you can learn how interfaces take input, work with structured data, respond to users, and turn results into experiences people can actually use.

$9
PDF BundleJavaScriptWeb DevelopmentAI Applications
  • 253-page illustrated PDF
  • 12 guided JavaScript chapters
  • Visual concept diagrams
  • Self-assessment quizzes
  • Bonus deep-dive sections
  • DOM, events, forms & application state
  • Browser projects, debugging & practical workflows

Coming soon

Keep learning

Related tutorials

Continue with nearby JavaScript topics and beginner-friendly explanations.