Skip to content

JavaScript Debugging

Effective debugging is a crucial skill for any JavaScript developer. Writing code without a good debugger can quickly become frustrating and inefficient.

All non-trivial JavaScript code is prone to errors. These can be syntax errors (caught by the JavaScript engine before execution), runtime errors (occurring during execution), or logical errors (where the code runs but produces incorrect results).

Often, when JavaScript encounters an unhandled runtime error, it might silently fail or stop executing, providing no clear indication of the problem’s source. A debugger helps you pinpoint these issues.

Debugging is the systematic process of finding and fixing errors (bugs) in your code.

All modern web browsers come equipped with powerful built-in developer tools that include a JavaScript debugger.

These tools allow you to:

  • View messages and errors logged to the console.
  • Set breakpoints to pause code execution at specific lines.
  • Step through code line by line to observe its flow.
  • Inspect the values of variables and expressions at any point.
  • Analyze the call stack to understand function execution paths.
  • Monitor network requests, inspect the DOM and CSS, and profile performance.

Typically, you can open developer tools by pressing F12. Common tabs used for debugging are ‘Console’ and ‘Sources’ (or ‘Debugger’).

Using console.log() and Other Console Methods

Section titled “Using console.log() and Other Console Methods”

The console object provides methods to log information to the browser’s console. This is one of the simplest forms of debugging.

  • console.log(): For general output of messages or variable values.
  • console.warn(): For displaying warnings.
  • console.error(): For displaying error messages.
  • console.info(): For informational messages (often styled like log).
  • console.table(): For displaying tabular data (arrays or objects) in a table format.
  • console.group() and console.groupEnd(): To group related log messages.
  • console.trace(): To output a stack trace.

Example:

<!DOCTYPE html>
<html lang="en">
<body>
<h1>Debugging with Console</h1>
<script>
let a = 5;
let b = 6;
let c = a + b;
console.log("Value of a:", a);
console.log("Value of b:", b);
console.log("Value of c (a + b):", c);
if (c > 10) {
console.warn("C is greater than 10!");
}
const user = { name: "Alice", age: 30 };
console.table(user);
</script>
</body>
</html>

Breakpoints are markers you set in your JavaScript code (via the ‘Sources’ or ‘Debugger’ panel in developer tools) where the browser will pause execution.

When execution is paused at a breakpoint, you can:

  • Inspect the current values of variables by hovering over them or using the ‘Scope’ panel.
  • Use the ‘Watch’ panel to monitor specific expressions.
  • Examine the ‘Call Stack’ to see the sequence of function calls that led to the current point.
  • Control execution: step over the current line, step into a function call, step out of the current function, or resume execution.

The debugger; statement, when placed in your JavaScript code, acts like a programmatic breakpoint.

If browser developer tools are open, JavaScript execution will pause at the debugger; line.

If no debugging tools are active, the debugger; statement has no effect.

Example:

let x = 15 * 5;
console.log("x before debugger:", x);
debugger; // Execution will pause here if dev tools are open
x = x + 10;
console.log("x after debugger:", x);
// document.getElementById("demo").textContent = x; // Example DOM update

This is useful for quickly pausing execution without manually finding the line in the Sources panel, especially in complex code or during development.

Accessing Developer Tools in Major Browsers

Section titled “Accessing Developer Tools in Major Browsers”

The general way to open developer tools is by pressing F12. Here are more specific ways:

  • Press F12 (Windows/Linux) or Cmd+Option+I (Mac).
  • Right-click on any page element and select “Inspect”.
  • Go to the Chrome menu (three vertical dots) -> More Tools -> Developer Tools.
  • Press F12 (Windows/Linux) or Cmd+Option+I (Mac).
  • Right-click on any page element and select “Inspect Element”.
  • Go to the Firefox menu (three horizontal lines) -> Web Developer -> Toggle Tools (or specific tools like Web Console, Debugger).
  • Press F12.
  • Right-click on any page element and select “Inspect”.
  • Go to the Edge menu (three horizontal dots) -> More tools -> Developer tools.
  • First, enable the Develop menu: Safari -> Preferences -> Advanced -> Check “Show Develop menu in menu bar”.
  • Then, press Cmd+Option+I or go to Develop -> Show Web Inspector.

Once open, navigate to the ‘Console’ tab to see messages and the ‘Sources’ (Chrome/Edge) or ‘Debugger’ (Firefox/Safari) tab to set breakpoints and step through code.

The term ‘bug’ for a flaw in a system dates back to Thomas Edison in the 1870s. The term ‘debugging’ for fixing computer issues was famously associated with Grace Hopper in the 1940s when a moth got stuck in a relay of the Harvard Mark II computer, causing it to malfunction. They ‘debugged’ the system by removing the actual insect.

Further reading: Check the official documentation for your browser’s developer tools for the most up-to-date features and guides.