J

JavaScript Handbook

Clean • Professional

JavaScript Debugging - Using Console & Debugger Effectively

3 minute

JavaScript Debugging (console, debugger)

Debugging in JavaScript is the process of finding and fixing errors in your code to make it run correctly. It helps identify issues like syntax errors, logic mistakes, or unexpected behavior. JavaScript provides built-in tools such as the console (for logging messages) and the debugger statement (to pause and inspect code). Using browser Developer Tools, you can check variables, set breakpoints, and step through code for smooth and error-free performance.

What is Debugging?

Debugging is the process of finding, understanding, and fixing errors (bugs) in your code. In JavaScript, these bugs can be:

  • Syntax errors: Mistakes in code structure (e.g., missing brackets or semicolons).
  • Runtime errors: Errors that occur while the program is running (e.g., calling an undefined function).
  • Logical errors: The code runs without crashing but produces incorrect results.

The goal of debugging is not only to fix errors but also to understand why they occurred to prevent them in the future.

Common Debugging Techniques

  1. Using console methods
  2. Using the debugger statement
  3. Browser Developer Tools
  4. Code linters and formatters
  5. Breakpoints and step-by-step execution

1. Using the console Object

The console object provides several helpful methods to print messages, debug values, and inspect program flow in your browser’s console (available via DevTools).

Common console Methods

console.log()

Used to display general information and variable values.

const name = "Alice";
console.log("User name is:", name);

console.error()

Displays an error message in red text, useful for reporting problems.

console.error("Something went wrong!");

console.warn()

Shows a warning message (yellow text), often used for potential issues.

console.warn("Deprecated method detected!");

console.table()

Displays array or object data in a table format for better readability.

const users = [
  { name: "John", age: 25 },
  { name: "Sara", age: 30 }
];
console.table(users);

console.group() and console.groupEnd()

Organizes console output into collapsible groups.

console.group("User Info");
console.log("Name: Alice");
console.log("Age: 22");
console.groupEnd();

console.time() and console.timeEnd()

Measures execution time for performance testing.

console.time("Loop time");
for (let i = 0; i < 1000000; i++) {}
console.timeEnd("Loop time");

2. Using the debugger Statement

The debugger keyword is a built-in JavaScript statement that pauses code execution at a specific point, allowing you to inspect variables, step through code, and analyze logic in browser DevTools.

Example:

function calculate(a, b) {
  debugger; // Code execution stops here
  return a + b;
}

calculate(5, 10);

3. Browser Developer Tools

Modern browsers (like Chrome, Edge, and Firefox) have powerful developer tools built in.

To open:

  • Windows/Linux: Press Ctrl + Shift + I or F12
  • Mac: Press Cmd + Option + I

Key Panels for Debugging:

  • Console: View logs, warnings, and errors.
  • Sources: Set breakpoints and step through code.
  • Network: Monitor API requests and responses.
  • Performance: Identify slow scripts or rendering issues.
  • Memory: Detect memory leaks and optimize performance.

You can click the line number in the “Sources” tab to set breakpoints, which pause execution automatically at that line.

4. Code Linters & Formatters

Tools like ESLint and Prettier help detect potential bugs and maintain code consistency.

  • ESLint: Catches syntax and logic errors before runtime.
  • Prettier: Ensures consistent code formatting, making debugging easier.

Example configuration (for ESLint):

npm install eslint --save-dev
npx eslint yourfile.js

5. Step-by-Step Debugging with Breakpoints

Breakpoints let you pause code at a certain line without changing the source code (unlike the debugger keyword).

Once paused, you can:

  • Examine variables
  • Evaluate expressions
  • Step into, over, or out of functions

This feature is especially useful in complex apps or loops to monitor variable changes.

Article 0 of 0