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. Sound familiar? Javascript is the most popular programming language according to . If you develop with Javascript, you’ve most likely came across a situation where you had to fix a bug or two. StackOverflow’s 2019 survey “No problem!” you say to your rubber ducky, let’s reproduce the bug and fix it with . Now, what if I told you that this is not the best practice? console.log() At the end of this article, I included a TL;DR summary. 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 (right click + inspect). Chrome DevTools Perhaps you’re already using it to , monitor console logs, and measure network performance. But did you know that Chrome has a powerful built in debugging feature where you can: view and modify HTML/CSS elements view source code set breakpoints step into, step over, and step out The rest of the article is a 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. step by step guide 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 Prerequisites Node.js version 10.9.0 or later. Step 1. Install Angular CLI install -g @angular/cli npm Step 2: Create a workspace and initial application ng my-app new Step 3: Run the application my- ng serve -- cd app open This will open your browser to the url localhost:4200 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. { Component } ; @Component({ : , : , : [ ] }) { author = getAuthor(); title = ; } { obj = { : }; obj; } import from '@angular/core' selector 'app-root' templateUrl './app.component.html' styleUrls './app.component.css' export class AppComponent `my-app by ` ${ .author} this ( ) function getAuthor const name 'songthamtung' return Look at the browser again and you should see bug! 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! [object Object] Debug Mode 🔴 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 — 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. author Press ▶️ to continue execution. 3. Step into, step over, and step out. These basic 3 steps is the bread and butter for debugging. is when the debugger steps into or inside a function Step into is when the debugger steps to the next line Step over is when the debugger steps outside the current function Step out Refresh the browser again and once the debugger pauses at your breakpoint, the function using the panel on the right hand side. This will take you to the function . Hover your mouse over obj and you will see undefined since we haven’t actually executed it yet. to execute the line and hover your mouse over obj again. This time, you should see a POJO. to return to the caller and now this time author is no longer undefined. step into getAuthor() Step over Step out 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 = ` -app ${this.author. }`; my by name 5. Deactivate breakpoints Click It changes blue to indicate that it’s active. While this is set, DevTools ignores any breakpoints you’ve set. Deactivate breakpoints. 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 to view your source code and set breakpoints. CMD + P For more information, check out on this topic. Google’s blog Thanks for reading . This was originally published on . Faun