As you know, we can call a function as many times as we want using the function name in JavaScript. But if you want to execute the same function a specific number of times, calling it with the function name isn't efficient.
In this situation, you can use loops to run your functions as many times as you want without manually calling them multiple times in your code.
I will show you how to use loops to call a function N number of times in JavaScript dynamically to accomplish a specific task.
Call a Function N Number of Times in JavaScript
In order to call a function multiple times in JavaScript, you have to use loops. You can use both the for
and while
loops for this purpose.
const someFunc = () => {
console.log('Hello world!')
}
const callFunction = (func, n) => {
for (let i = 1; i <= n; i++) {
func()
}
}
callFunction(someFunc, 3)
Suppose you have a function called someFunc
which you want to call multiple times programmatically. For that, you can create another function that accepts 2 parameters: a function to call and the number of times you want to call.
Here the callFunction()
function takes two parameters: func
, which is the function you want to call multiple times, and n
, which is the number of times you want to call the function.
Inside this callFunction()
function, you can use for loop to iterate n
times. It will run the func()
function for each iteration.
Now execute the callFunction()
function with someFunc
(the name of a function) and the number of times you want to run it for example "3". This will execute the someFunc
function 3 times.
const callFunction = (func, n) => {
let i = 1
while (i <= n) {
func()
i++
}
}
callFunction(someFunc, 3)
// Hello world!
// Hello world!
// Hello world!
You can also use the while
loop if you don't want to use the for
loop. Everything else will be the same. Just change the loop.
Also Read: All Methods to Loop Through Objects in JavaScript
Run a JavaScript Function Multiple Times with Arguments
Sometimes you might want to run functions multiple times that accept arguments. Let's see how you can execute a function with arguments many times inside a loop.
const add = (a, b) => {
console.log(a + b)
}
const callFunction = (func, n, ...args) => {
for (let i = 1; i <= n; i++) {
func(...args)
}
}
callFunction(add, 3, 10, 5)
// 15
// 15
// 15
You have an add()
function that takes 2 numbers as parameters and adds them. Now if you want to execute this function inside a loop you also need to pass those parameters.
In the callFunction()
function, you can add a third parameter along with func
and n
to accept additional parameters. The ...args
is a rest parameter that allows us to pass any number of additional arguments to the func
function.
The main advantage of accepting parameters with the rest parameter techniques is that you can also call any other functions with any number of arguments using callFunction()
function.
Inside the callFunction()
function, the args
will contain all the arguments as an array. Therefore, you can pass those arguments to the func()
function using spread syntax (...) in JavaScpit.
Also Read: Self Invoking Functions in JavaScript - Why Should You Use?
Execute a Function Specific Number of Times with Return Value
So far we have seen 2 ways to run functions multiple times that execute some JavaScript code. But many times, we write functions that return something every time we call them.
How can you call a function a specific number of times that returns a value and get those returned values?
You can modify the callFunction()
function in such a way that it will be able to handle both types of functions. It can be done very easily using an if
check.
const add = (a, b) => {
return a + b
}
const someFunc = () => {
console.log('Hello world!')
}
You have an add()
function, which accepts arguments and returns a value. You also have the someFunc()
function, which just executes some JavaScript code but doesn't return anything.
You can run both of these functions as many times as you want using the callFunction()
function.
const callFunction = (func, n, isReturn = false, ...args) => {
const returnValues = []
for (let i = 1; i <= n; i++) {
if (isReturn === true) {
const returned = func(...args)
returnValues.push(returned)
} else {
func(...args)
}
}
return returnValues
}
callFunction(someFunc, 2)
// Hello world!
// Hello world!
const values = callFunction(add, 4, true, 5, 5)
console.log(values)
// [ 10, 10, 10, 10 ]
The callFunction()
function accepts an additional parameter called isReturn
which is false
by default. When you pass a function that returns a value when executed, you will set this isReturn
parameter to true
.
If isReturn
is true
, you will capture the return value from the func()
function and push it to the returnValues
array. If it is false
, you will just call the func()
function.
Finally, return the returnvalues
array from the callFunction()
function so that you can get all the return values. If the func()
function doesn't return anything, this will be an empty array.
Now you can call both someFunc
and add
functions using the callFunction()
function. While calling the add
function multiple times, set the isReturn
parameter to true
and then pass additional arguments.
Also Read: Factory Function in JavaScript Explained with Examples
Conclusion
You have seen 3 techniques to run functions multiple times in this blog post. You can use any one of those depending on the nature of the function that you want to execute.
The last technique is a little complex but much more flexible than the previous two. You can call any type of function with this. It can run a function that accepts arguments and returns values.
Create your own function that can call other functions N number of times in JavaScript dynamically without using the function names.