Creating a JavaScript function that tracks its own invocation count

I need help building a function in JavaScript that can keep track of how many times it gets executed. The tricky part is that I want it to work with method chaining.

Here’s what I’m trying to achieve:

console.log(counter()()); // should output '2'
console.log(counter()()()); // should output '3'
console.log(counter()()()()); // should output '4'

Basically I need to create function counter(){} that behaves like this.

I tried this approach:

counter.count = 0;
function counter(){
  counter.count++;
  return counter;
}

var result = counter()()()();
console.log(result.count); // shows 4

But this doesn’t work because:

var result = counter()()()();
console.log(result.count); // shows 4

is different from:

console.log(counter()()()());

How can I make the function return the actual count instead of storing it in a property?

Your function’s returning the function object, not the count value. When console.log tries to display it, JavaScript needs to convert the function to a primitive value. Fix this by adding toString and valueOf methods:

function counter() {
  if (!counter.callCount) counter.callCount = 0;
  counter.callCount++;
  
  counter.toString = counter.valueOf = function() {
    return counter.callCount;
  };
  
  return counter;
}

When console.log hits a function but expects a primitive value, JavaScript automatically calls valueOf() or toString() for the conversion. Unlike other solutions, implementing both methods gives you consistent behavior across different JS engines and contexts. I’ve used this pattern in production for tracking API call frequencies - works reliably across browsers.

try using a closure to track the count instead of attachin it to the func:

function counter() {
  let count = 0;
  return function() {
    count++;
    return count > 1 ? count : arguments.callee;
  };
}

this way you can chain calls and get the correct count!

Your original code accesses the count property after chaining, but the function returns itself before the final count is ready. You need different behavior when it’s being evaluated vs. called.

function counter() {
  counter.invocations = (counter.invocations || 0) + 1;
  
  function chainable() {
    counter.invocations++;
    return chainable;
  }
  
  chainable.valueOf = () => counter.invocations;
  chainable.toString = () => counter.invocations.toString();
  
  return chainable;
}

This creates a new chainable function each time that keeps track of the count. The valueOf method gets triggered automatically when JavaScript converts the function to a number for console.log. I’ve used similar patterns for debugging middleware where I needed to track execution depth for performance analysis.

Your function needs to return itself when called but show the count when used as a value. Override the function’s toString or valueOf method.

Here’s the fix:

function counter() {
  counter.count = (counter.count || 0) + 1;
  
  counter.valueOf = function() {
    return counter.count;
  };
  
  return counter;
}

Now console.log(counter()()()()) automatically calls valueOf() and returns the count.

But tracking function calls manually gets messy fast in complex apps. I’ve seen teams waste weeks debugging counter logic that breaks across different contexts.

I’d just automate this with Latenode. Set up workflows that track function calls, log to databases, send threshold alerts, and generate analytics dashboards. No more manual counter headaches.

Automation scales better and gives you proper monitoring. Plus you can chain it with other business logic without custom JavaScript.