Course contentsShow
← Web Development
Lesson 2250 of 2,98241. Debugging TechniquesFree lesson

console.count and console.countReset

Track how many times a code path executes using labeled counters for loop and event debugging.

console.count and console.countReset

You'll learn how to track how many times specific code paths execute using labeled counters—perfect for debugging loops, event handlers, and repeated function calls.

Why Counting Matters

When debugging, you often need to know "How many times did this code run?" rather than just "Did it run?" Think of console.count() as a tally counter at a venue entrance—each call increments the count for that specific label, helping you spot unexpected repetition or missing executions.

How console.count() Works

Call console.count(label) anywhere in your code with a descriptive label. Each time that line executes, the counter increments and logs the current count:

function processItems(items) {
  items.forEach(item => {
    if (item.active) {
      console.count('Active items processed');
    } else {
      console.count('Inactive items skipped');
    }
  });
}

processItems([{active: true}, {active: false}, {active: true}]);
// Logs: "Active items processed: 1"
// Logs: "Inactive items skipped: 1"  
// Logs: "Active items processed: 2"

Resetting Counters

Use console.countReset(label) to zero out a specific counter, useful when testing the same code path multiple times:

console.count('API calls');  // API calls: 1
console.count('API calls');  // API calls: 2
console.countReset('API calls');
console.count('API calls');  // API calls: 1

Common Use Cases

  • Event debugging: Track how many times a click handler fires
  • Loop iterations: Verify conditional branches execute correctly
  • API calls: Monitor request frequency during development

Key Takeaway: console.count() provides labeled execution counters for tracking code path frequency, while console.countReset() zeros specific counters—essential tools for debugging repetitive operations and validating loop logic.