How to find out how many Times a Function is Called with JavaScript ? Last Updated : 23 Jul, 2025 Comments Improve Suggest changes Like Article Like Report In JavaScript, a function can be called multiple times wherever you need the same kind of functionality. You can also keep track of the function calls to find out how many times it is called in the whole code using the below approaches. Table of Content Using a Counter VariableUsing a Wrapper FunctionUsing a Proxy ObjectUsing a Counter VariableIn this approach, a counter variable is used inside the function which will be incremented by one every time the function is called and the final value of the counter variable will be equal to the function calls. Example: The below code implements the counter variable to find out the number of times a function is called. JavaScript let counter = 0; function myFunction() { counter++; } // Calling the function myFunction(); myFunction(); myFunction(); myFunction(); console.log("Function called:", counter, "times"); OutputFunction called: 4 times Using a Wrapper FunctionWe can wrap the original function inside another function that increments a counter before calling the original function. Thus finding out how many times a function is called within JavaScript program. Example: The below code uses a wrapper function to wrap the original function to count the number of times the function has been called. JavaScript let counter = 0; function myFunctionWrapper() { counter++; myFunction(counter); } function myFunction(count) { console.log("Function called:", count, "times"); } myFunctionWrapper(); myFunctionWrapper(); myFunctionWrapper(); OutputFunction called: 1 times Function called: 2 times Function called: 3 times Using a Proxy ObjectWe use Proxy object to intercept calls to the original function and each time the function is called through the proxy, it logs the number of times the function has been called and thus counting the number of times the function has been called. Example: The below code uses a proxy object to find out the number of times a function is called. JavaScript let counter = 0; const myFunctionProxy = new Proxy(myFunction, { apply: function (target, thisArg, args) { counter++; return Reflect.apply (target, thisArg, args); } }); function myFunction() { console.log("Function called:", counter, "times"); } myFunctionProxy(); myFunctionProxy(); myFunctionProxy(); myFunctionProxy(); OutputFunction called: 1 times Function called: 2 times Function called: 3 times Function called: 4 times Comment More infoAdvertise with us Next Article Function that can be called only once in JavaScript M mishraaabcf9 Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Similar Reads How to find out the caller function in JavaScript? In this article, we see the methods to find out the caller function in Javascript. Sometimes, the developer may want to modify how a function works on the basis of its caller function. To find out the caller function name, we will use the Function object's caller property. Property: Function.caller 1 min read How to Execute setInterval Function without Delay for the First Time in JavaScript? When working with JavaScript, the setInterval function is a powerful tool for executing a block of code repeatedly at specified intervals. The setInterval() method always invokes the function after the delay for the first time. Approach 1: Calling the function once before executing setInterval:Invok 2 min read How to call a function that return another function in JavaScript ? The task is to call a function that returns another function with the help of JavaScript is called a Currying function, a function with numerous arguments that can be converted into a series of nesting functions with the help of the currying method. Approach:Define Outer Function:Create an outer fun 2 min read Function that can be called only once in JavaScript In JavaScript, you can create a function that can be called only once by using a closure to keep track of whether the function has been called before. JavaScript closure is a feature that allows inner functions to access the outer scope of a function. Closure helps in binding a function to its outer 3 min read How to count the number of times a button is clicked using JavaScript ? At times, it becomes necessary to monitor the number of times a user clicks a button. In this article, we are going to learn how to count the number of times a button is clicked using JavaScript Below are the approaches to count the number of times a button is clicked using JavaScript Table of Conte 3 min read How to call a function repeatedly every 5 seconds in JavaScript ? In JavaScript, the setInterval() method allows you to repeatedly execute a function or evaluate an expression at specified intervals. This method is particularly useful for performing periodic tasks like updating a user interface or making repeated API calls.Syntax:Â setInterval(function, millisecond 2 min read Like