Skip to content

ES6 - Debugging

Mistakes, or “bugs,” are an inevitable part of programming. Debugging is the process of finding and fixing these bugs. Modern JavaScript development offers powerful tools and techniques to make this process more efficient.

All modern web browsers (Chrome, Firefox, Edge, Safari) come with built-in Developer Tools, which are essential for debugging client-side JavaScript. You can typically open them by pressing F12 or right-clicking on a webpage and selecting “Inspect” or “Inspect Element”.

  • Console: View messages logged by your script (using console.log(), console.error(), etc.), inspect objects, and execute JavaScript expressions interactively.
  • Sources (or Debugger) Panel: View your source code, set breakpoints to pause execution at specific lines, step through code line-by-line (step over, step in, step out), inspect variable values, and examine the call stack.
  • Network Panel: Inspect network requests and responses.
  • Application (or Storage) Panel: Inspect cookies, local storage, session storage, etc.

The console object provides several methods for logging information:

  • console.log(): General output of logging information.
  • console.info(): Informative logging information.
  • console.warn(): Outputs a warning message.
  • console.error(): Outputs an error message.
  • console.table(): Displays tabular data as a table.
  • console.group() / console.groupEnd(): Groups related log messages.
  • console.assert(assertion, message): Logs a message if the assertion is false.
function greet(name) {
console.log(`Preparing to greet ${name}`);
if (!name) {
console.warn('Name is not provided!');
return;
}
console.log(`Hello, ${name}!`);
}
greet('World');
greet();

Setting Breakpoints and Stepping Through Code

Section titled “Setting Breakpoints and Stepping Through Code”

In the Sources/Debugger panel of your browser’s DevTools:

  • Set a breakpoint: Click on the line number where you want execution to pause.
  • Step Over: Execute the current line and move to the next line in the current function. If the current line is a function call, it executes the entire function without stepping into it.
  • Step Into: If the current line is a function call, step into that function and pause at its first line.
  • Step Out: Continue execution until the current function returns, then pause.
  • Watch Expressions: Add variables or expressions to monitor their values as you step through code.
  • Call Stack: Shows the sequence of function calls that led to the current point of execution.

You can also programmatically trigger a breakpoint by adding the debugger; statement in your JavaScript code. If Developer Tools are open, execution will pause at this statement.

function calculateSum(a, b) {
let result = a + b;
debugger; // Execution will pause here if DevTools are open
console.log(`The sum is ${result}`);
return result;
}
calculateSum(5, 10);

Linters like ESLint analyze your code for potential errors, style issues, and bad practices without actually running it. This is called static analysis. Integrating ESLint into your development workflow (often via a code editor extension) can help catch bugs early.

Key benefits of ESLint:

  • Finds syntax errors and runtime errors.
  • Enforces code style consistency.
  • Identifies problematic patterns (e.g., unused variables).
  • Highly configurable with plugins and shareable configurations.

You typically install ESLint as a development dependency in your project: npm install eslint --save-dev or yarn add eslint --dev. More info: eslint.org.

Node.js has a built-in debugger. You can start it using the inspect flag:

node --inspect my_script.js
node --inspect-brk my_script.js

This starts a debugging server that you can connect to using tools like Chrome DevTools (open chrome://inspect in Chrome) or the VS Code debugger.

VS Code has excellent built-in debugging support for Node.js. You can create a launch configuration (launch.json file) to easily start debugging sessions, set breakpoints, inspect variables, and step through code directly within the editor.

For detailed guidance, refer to the VS Code debugging documentation.

  • Understand the Error: Read error messages carefully. They often provide clues about the line number and type of error.
  • Reproduce the Bug: Consistently reproduce the bug to understand the conditions under which it occurs.
  • Isolate the Problem: Simplify your code or comment out sections to pinpoint where the bug is located (the “divide and conquer” approach).
  • Write Testable Code: Modular code (e.g., small functions with clear responsibilities) is easier to test and debug.
  • Use Version Control: Git allows you to revert to previous working states if a change introduces a bug.
  • Rubber Duck Debugging: Explain your code line-by-line to someone else, or even to an inanimate object (like a rubber duck). This process can often help you spot the error yourself.