5 Ways to Add Breakpoints on Chrome Devtools
Tags: javascript, webdev
Published: 2022-01-07 07:43:51

Debug is a skill that every developer must master, and adding breakpoints to the code is the basic of debugging. This article will share 5 ways to add breakpoints in Chrome DevTool.

---

## 1# Add breakpoints directly

Go to Source Tag of Chrome Devtools, click the line number where the code is located to add a breakpoint.


![111](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1tnkgsaf3kypsvu0j34e.png)

This should be the most common way for everyone to add breakpoints.

## 2# Conditional breakpoints

But sometimes, we want a breakpoint to take effect only under certain conditions. At this time, we can take the following approach.

- Right-click the line number column
- Choose Add conditional breakpoint
- Enter your condition in the dialog

For Example, we want to pause the code when i is greater than 10,

```js
for(let i=0; i<20; i++){
     console.log(`i * i = ${i * i}`)
}
```

We can:


![Image desceetion](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ezezng0nkv8bjy3qcwud.gif)


## 3# DOM change breakpoints

In some cases, we don't want to pause the code at a certain location, but only when a DOM element changes. At this time, we can do this:

- Click the Elements tab.

- Go to the element that you want to set the breakpoint on.

- Right-click the element.

- Hover over Break on then select Subtree modifications, Attribute modifications or Node removal.

For example, we want to pause the code when the hello element will change:

```js
<div id="hello">
    Hello 2022.
</div>
<script>
    document.getElementById("hello").onclick = (event) => {
      event.target.innerText = new Date().toString()
    }
</script>
```

We can:


![Image descriptweion](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w2z47n13c7rdbeqot7i0.gif)

Note:

- Subtree modifications. Triggered when a child of the currently-selected node is removed or added, or the contents of a child are changed. Not triggered on child node attribute changes, or on any changes to the currently-selected node.

- Attributes modifications: Triggered when an attribute is added or removed on the currently-selected node, or when an attribute value changes.

- Node Removal: Triggered when the currently-selected node is removed.

## 4# XHR/Fetch breakpoints

If you want to pause the code when JavaScript is trying to make an HTTP request to a URL, we can do this:

- Click the Sources tab.
Expand the XHR Breakpoints pane.
Click Add breakpoint.
Enter the string which you want to break on. DevTools pauses when this string is present anywhere in an XHR's request URL.
Press Enter to confirm.

For example, we want to pause the code when the script tries to requesting `api.github.com` .

```js
<body>
  <div id="hello">
    Hello 2022.
  </div>
  <script>
    fetch("https://api.github.com")
     .then(res => {
       console.log(res)
     })
  </script>
</body>
```

We can:


![Im33](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m1apj7iv413q5f7d4cik.gif)

## 5# Event listener breakpoints

Of course, we can also pause the code when a certain event is triggered.

```js
<body>
  <div id="hello">
    Hello 2022.
  </div>
  <script>
    document.getElementById("hello").onclick = (event) => {
      console.log('hello 2022')
    }
  </script>
</body>
```

If you want to pause the code after the hello element is clicked, then we can do this:

![Image 444](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mj8sc8oykpurbtaedbih.gif)