Course contentsShow
← JavaScript
Lesson 1488 of 2,72332. JavaScript in the BrowserFree lesson

Console.count and Execution Tracking

Count how many times a particular log point is reached, helpful for loop and callback debugging.

Console.count and Execution Tracking

You'll learn how to use console.count() to track how many times specific code paths execute—essential for debugging loops, callbacks, and repeated function calls.

What Is Console.count?

console.count() acts like a tally counter at a theme park entrance. Every time you pass through a checkpoint, it increments and displays the count. In code, each time a particular console.count() statement runs, JavaScript tracks and displays how many times that label has been encountered.

Basic Syntax

function processItem(item) {
  console.count('processItem called');
  // ... rest of logic
}

processItem('apple');
processItem('banana');
processItem('cherry');

// Output:
// processItem called: 1
// processItem called: 2
// processItem called: 3

Why This Matters

When debugging loops or callbacks, you often wonder: "Is this even running?" or "How many times is this executing?" Rather than manually adding variables and logging them, console.count() does the bookkeeping automatically.

const items = ['a', 'b', 'c'];
items.forEach(item => {
  console.count('forEach iteration');
  if (item === 'b') {
    console.count('found b');
  }
});

// Output:
// forEach iteration: 1
// forEach iteration: 2
// found b: 1
// forEach iteration: 3

Resetting the Counter

Use console.countReset() to clear a specific counter back to zero:

console.count('clicks');
console.count('clicks');
console.countReset('clicks');
console.count('clicks');

// Output:
// clicks: 1
// clicks: 2
// clicks: 1  (reset!)

Default Label

If you omit the label, JavaScript uses "default" as the counter name—useful for quick, temporary debugging.

Key Takeaway: Use console.count(label) to automatically track how many times specific execution paths run, making loop and callback debugging faster and more precise.