How to Debug JavaScript in VS Code: A Beginner’s Guide to Breakpoints

Your JavaScript runs without an error, but the answer is wrong. You read the function again, add a few console messages and still cannot see where things went off track.

This is where a debugger helps. It lets you pause a program, inspect its values and follow what happens one step at a time.

That is useful whether you wrote the code yourself or an AI assistant helped with it. A suggested explanation is one thing; seeing the actual values while the code runs gives you something to check.

In this guide, we will find and fix a small calculation bug using VS Code’s built-in JavaScript debugger.

What you need

  • The desktop version of VS Code.
  • Node.js installed on your computer.
  • A folder containing a JavaScript file.

Node.js runs JavaScript outside the browser. We will use it here to keep the example small, without setting up a webpage or a React project.

To check whether Node.js is available, open a terminal and run:

node --version

You should see a version number. If the command is not found, install a supported Node.js version before continuing. You may need to restart VS Code after installation.

You do not need an additional JavaScript debugging extension or an AI subscription for this example.

1. Create a program with a small bug

Create a practice folder and open it in VS Code using File → Open Folder.

Inside it, create a file called app.js and add:

function calculateTotal(price, quantity) {
  const total = price + quantity;
  return total;
}

const result = calculateTotal(20, 3);
console.log(result);

We want the total price for three items costing 20 each. The expected result is 60.

Open VS Code’s terminal using Terminal → New Terminal. Make sure it is in your practice folder, then run:

node app.js

The output is:

23

There is no syntax error. JavaScript can run the instructions we gave it. The problem is that those instructions do not calculate what we intended.

2. Tell VS Code what to debug

We will add a small configuration so VS Code knows which program to start.

In your practice folder, create a folder named .vscode. Inside it, create launch.json.

Paste this into .vscode/launch.json:

{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "node",
      "request": "launch",
      "name": "Debug app.js",
      "program": "${workspaceFolder}/app.js",
      "skipFiles": ["<node_internals>/**"]
    }
  ]
}

The settings tell VS Code to launch app.js using its Node.js debugger.

${workspaceFolder} means the folder you opened in VS Code. You do not need to replace it with your computer’s full file path.

The skipFiles setting helps keep stepping focused on our code instead of Node.js internals.

If your project already has a launch.json file, do not replace its existing settings. This guide uses a separate practice folder.

3. Set a breakpoint

Open app.js. Click in the space immediately to the left of the line number for:

const total = price + quantity;

A red dot should appear. That is a breakpoint: a place where the debugger should pause the program.

A line breakpoint normally pauses before the marked line executes. That means we can inspect the inputs before the calculation happens.

4. Start the debugger

Open the Run and Debug view from VS Code’s sidebar.

Select Debug app.js from the configuration list and click the green start button. You can also use Run → Start Debugging.

The program should pause at the breakpoint. The current line will be highlighted, and debugging controls will appear.

This is different from running node app.js in a normal terminal. For this example, start the configured debug session so VS Code can pause at your breakpoint.

5. Inspect the values

While the program is paused, hover over price and quantity.

You should find:

  • price is 20.
  • quantity is 3.

You can also expand the relevant section in the Variables area of the Run and Debug view.

This answers an important question: did the function receive the values we expected?

Here, it did. We can now focus on the calculation rather than changing the function call.

The assignment to total has not happened yet, so do not expect it to contain the calculated result at this pause.

6. Step over the calculation

Click Step Over on the debugging toolbar. You can hover over the buttons to see their names.

Step Over runs the current line and pauses at the next stopping point without taking you through the inside of a function called on that line.

The debugger should move to:

return total;

Now inspect total. Its value should be 23.

We have narrowed the problem down:

  • The inputs are correct.
  • The calculation produces 23.
  • The code uses +, which adds 20 and 3.

For the total price, we need multiplication.

7. Fix the code and run it again

Stop the debug session using the stop button.

Change the calculation to:

const total = price * quantity;

The complete program is now:

function calculateTotal(price, quantity) {
  const total = price * quantity;
  return total;
}

const result = calculateTotal(20, 3);
console.log(result);

Save the file and start Debug app.js again.

At the breakpoint, check the inputs. Step Over and inspect total. It should now be 60.

Click Continue to let the program finish. With this configuration, the output should appear in the Debug Console:

60

We have checked both the value inside the function and the final output.

What do the main debugging controls do?

You only need a few controls to start:

  • Continue: Run until another breakpoint is reached or the program finishes.
  • Step Over: Run the current line without following a called function’s internal steps.
  • Step Into: Follow execution into a function when debugging support allows it.
  • Step Out: Finish the current function and pause back in its caller.
  • Stop: End this debugging session.

To practise Step Into, move the breakpoint to:

const result = calculateTotal(20, 3);

Remove the earlier breakpoint, start again and choose Step Into when the program pauses. You should enter calculateTotal() and be able to follow its calculation.

Watch a value while you step

The Watch area lets you add an expression and see its value when the debugger pauses.

While paused inside calculateTotal(), add:

price * quantity

It should show 60 for our inputs.

Watch expressions are evaluated in the selected debugging context. If you pause outside the function, its local variables may no longer be available.

Start with simple expressions. Expressions that call functions or change values can affect the program you are investigating.

What if the breakpoint is not hit?

Check these before changing the program:

  • Did you save app.js?
  • Did you start Debug app.js, rather than Run Without Debugging?
  • Does launch.json point to the correct file?
  • Is the breakpoint enabled and placed on an executable line?
  • Does the program actually reach that line?

If the breakpoint appears hollow or unbound, hover over it for more information.

React and browser applications may also need source-map and browser-debugging settings. Those connect the running application to the source files in your editor. This example avoids that setup by using one JavaScript file with Node.js.

Where does AI fit into this?

An AI assistant can suggest why a calculation is wrong, explain unfamiliar code or help you list cases to check.

The debugger lets you compare those suggestions with what the program actually does.

If an assistant says the input is a string, pause and inspect it. If it says a function is never called, set a breakpoint inside that function and check.

A useful question to ask after gathering evidence is:

I expected 60. At the breakpoint, price is the number 20 and quantity is the number 3. After this calculation, total is 23. Explain why this line produces that result.

That gives the assistant a specific problem and observed values. Check any proposed fix by running the program again.

A breakpoint does not prove the whole application is correct. It shows what happened on the path you ran with those inputs.

Try another case

Change the function call to:

const result = calculateTotal(15, 4);

Write down the expected answer, then use the debugger to check it.

Try a quantity of zero too. As the program grows, decide which inputs are allowed and add tests for the behaviour you need to preserve.

You do not have to stop using console.log(). Breakpoints give you another option when you need to inspect values together or follow the order in which code runs.

Start with one small function. Once pausing and stepping feel familiar, the debugger becomes much less intimidating.

Further reading:

No comments