Menu

Using 'debugger' to set breakpoints in JavaScript

Using 'debugger' to set breakpoints in JavaScript

Errors are a part of the coding game and tracking them can sometimes take up most of the job.

This is why most programming IDE's offer some form of debugging system, typically through breakpoints.

What are breakpoints? Think of them as pauses that you can add to your code temporarily to see just what is happening at that point in time.

All modern browsers currently offer built-in JavaScript debugging with breakpoints.

The debugger statement, allows you to directly add breakpoints right in your code.

function driveCar(){
    let direction = 'left';
    debugger;
    turnWheel(direction);
}

In the example above, assuming that the developer tools were open in your browser tab, you would get an instant breakpoint stop on the second line.

This is a great way to save your breakpoints while you are working on a project, as otherwise the breakpoints are saved on the browser itself.

But remember to remove them before they launch to production!

Walter G. author of blog post
Walter Guevara is a Computer Scientist, software engineer, startup founder and previous mentor for a coding bootcamp. He has been creating software for the past 20 years.

Get the latest programming news directly in your inbox!

Have a question on this article?

You can leave me a question on this particular article (or any other really).

Ask a question

Community Comments

No comments posted yet

Add a comment