How to Use Chrome to Debug JavaScript

If you console.log() when you debug, you’re doing it wrong. There’s an easier way and it’s right in the palm of your browser.

Songtham Tung
Aug 4 · 5 min read

Sound familiar?

Javascript is the most popular programming language according to StackOverflow’s 2019 survey . If you develop with Javascript, you’ve most likely came across a situation where you had to fix a bug or two.

“No problem!” you say to your rubber ducky, let’s reproduce the bug and fix it with console.log(). Now, what if I told you that this is not the best practice?

Console logging works, but there’s a better way.

Instead of console.logging and restarting every time you want to debug, you can instead use Chrome DevTools (right click + inspect).

Perhaps you’re already using it to view and modify HTML/CSS elements, monitor console logs, and measure network performance. But did you know that Chrome has a powerful built in debugging feature where you can:

  • view source code
  • set breakpoints
  • step into, step over, and step out

The rest of the article is a step by step guide on how to use Chrome’s debugging feature with Angular — but feel free to follow along with your existing projects on any javascript frontend framework.

Setting up your environment.

NOTE: If you already have an existing project, skip to the next section.

In this section, we will quickly set up an Angular app using their official guide.

Node.js version 10.9.0 or later.

npm install -g @angular/cli
ng new my-app
cd my-app
ng serve --open

This will open your browser to the url localhost:4200

Angular v8.2

Create the bug 🐛

For the purposes of this demonstration, let’s create a bug and then debug it ;).

Open your favorite text editor and navigate to src/app/app.component.ts

Replace the lines with the following and save.

Added string interpolation to title and function getAuthor() that returns an object

Look at the browser again and you should see

Bug!

[object Object] is simply the default return value when converting a POJO (plain old javascript object) to a string. This is not a desired outcome — so let’s fix it!

Debug it! 🔴

Opening our source code with Chrome DevTools

Using Chrome, right click > inspect > sources > cmd + p > search file

If done correctly, this will take you to the source code, where the bug lurks.

Setting breakpoints is vital to debugging effectively. A breakpoint is an intentional pause in a program, which allows the developer to inspect the internal state of the application at that moment. You can use it to view variables and perform other debugging functions.

A breakpoint is an intentional pause in a program, which allows the developer to inspect the internal state of the application at that moment.

To set a breakpoint, click the line number where you want the program to pause. In the example below, we set it at line 9.

Setting breakpoint will highlight the line number and add it to the list on the right panel

Refresh the browser and you should see “Paused in debugger”.

Using a breakpoint to inspect a variable

Hover your mouse over the variable author— it should be undefined. The reason that it’s undefined is because the program hasn’t reached this line yet. It finished executing line 8 and is about to reach line 9.

Press ▶️ to continue execution.

These basic 3 steps is the bread and butter for debugging.

  • Step into is when the debugger steps into or inside a function
  • Step over is when the debugger steps to the next line
  • Step out is when the debugger steps outside the current function
Step in, step over, and step across in action

Refresh the browser again and once the debugger pauses at your breakpoint, step into the function using the panel on the right hand side. This will take you to the function getAuthor(). Hover your mouse over obj and you will see undefined since we haven’t actually executed it yet. Step over to execute the line and hover your mouse over obj again. This time, you should see a POJO. Step out to return to the caller and now this time author is no longer undefined.

Great — so we now know that author object has value. How do we fix it?

Replace line 10 with the following and save.

title = `my-app by ${this.author.name}`;
Remember to deactivate breakpoints

Click Deactivate breakpoints. It changes blue to indicate that it’s active. While this is set, DevTools ignores any breakpoints you’ve set.

Refresh the page.

Fixed!

Closing

Congratulations! You now know how to use Chrome DevTools to debug efficiently. While console.log() does have a place in programming, it’s limited to modifying the source code, restarting the program, and nonstop execution. Chrome’s built in debugging tool addresses these disadvantages and offers you the ability to stop, inspect, and investigate what’s happening in the program at the specific point in time.

TL;DR

Open inspector, click the Sources tab, and CMD + P to view your source code and set breakpoints.


For more information, check out Google’s blog on this topic.

Thanks for reading — and please follow me here on Medium for more interesting software engineering articles!

P.S. We’re hiring! Explore our current openings at https://panyastudios.com

Faun

The Must-Read Publication for Aspiring Developers & DevOps Enthusiasts

Songtham Tung

Written by

Technical Product Director @ Geddit | SF ✈️ BKK

Faun

Faun

The Must-Read Publication for Aspiring Developers & DevOps Enthusiasts

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade