In this tutorial, our goal is to equip you with the skills needed to debug JavaScript code effectively using Browser Developer Tools, commonly known as DevTools. By the end of this tutorial, you will be able to navigate the console, use breakpoints to pause code execution at specific lines, and trace and resolve JavaScript errors.
Prerequisites:
- Basic knowledge of HTML, CSS, and JavaScript
- Familiarity with any web browser (we'll use Chrome for this tutorial)
In Chrome, you can open DevTools by right-clicking anywhere on a webpage, selecting "Inspect" or using the shortcut Ctrl + Shift + I
on Windows or Cmd + Option + I
on Mac.
The console is where JavaScript can be written and executed. It's also where errors and logs are displayed. To print something to the console, you can use console.log()
. For instance:
console.log("Hello, World!");
Breakpoints are markers you set on JavaScript code lines to pause execution. This feature is handy when you need to inspect variables or code behavior at specific moments. To set a breakpoint, open the "Sources" tab in DevTools, find your JavaScript file, and click on the line number.
JavaScript errors are displayed in the console, often with a stack trace, which shows the order of function calls leading to the error. You can click on any of these calls to inspect the state of your code at that moment.
console.log("Hello, World!"); // This will print "Hello, World!" to the console.
// In your JavaScript file
function helloWorld() {
let greeting = "Hello, World!";
console.log(greeting); // Set a breakpoint on this line in DevTools using the "Sources" tab.
}
helloWorld();
function faultyFunction() {
throw new Error("Something went wrong!");
// An error is thrown here, which can be traced in the console.
}
faultyFunction();
In this tutorial, we learned how to open DevTools in Chrome, use the console for logging and error tracing, and set breakpoints to pause code execution. Your next steps could include learning more about other DevTools features, such as the Elements panel for inspecting and modifying HTML and CSS, or the Network panel for inspecting network activity.
Exercise 1: Write a simple JavaScript program and use console.log()
to print your name to the console.
Exercise 2: Add a function to the program from Exercise 1. Set a breakpoint within this function and inspect the local variables when the breakpoint is hit.
Exercise 3: Modify the function from Exercise 2 to throw an error. Trace this error in the console.
Note: Don't worry if you can't solve these exercises right away. Debugging is a skill that improves with practice! Keep experimenting with different scenarios and consult the DevTools documentation for additional help.
Solution 1:
console.log("Your Name"); // Replace "Your Name" with your actual name
Solution 2:
function sayHello() {
let greeting = "Hello, Your Name!"; // Set a breakpoint here to inspect the `greeting` variable
console.log(greeting);
}
sayHello();
Solution 3:
function faultyGreeting() {
let greeting = "Hello, Your Name!";
console.log(greeting);
throw new Error("Oops!"); // This error can be traced in the console
}
faultyGreeting();