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.
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?
At the end of this article, I included a TL;DR summary.
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:
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.
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.
Prerequisites
Node.js version 10.9.0 or later.
Step 1. Install Angular CLI
npm install -g @angular/cli
Step 2: Create a workspace and initial application
ng new my-app
Step 3: Run the application
cd my-app
ng serve --open
This will open your browser to the url localhost:4200
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.
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
author = getAuthor();
title = `my-app by ${this.author}`;
}
function getAuthor() {
const obj = { name: 'songthamtung' };
return obj;
}
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!
1. Inspect the sources
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.
2. Set breakpoints
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.
3. Step into, step over, and step out.
These basic 3 steps is the bread and butter for debugging.
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?
4. Fix the bug
Replace line 10 with the following and save.
title = `my-app by ${this.author.name}`;
5. 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!
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.
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 . This was originally published on Faun.