Open In App

How to iterate over a callback n times in JavaScript?

Last Updated : 15 Oct, 2025
Comments
Improve
Suggest changes
2 Likes
Like
Report

Given a callback function, we have to iterate over a callback n times. The callback is a function that is passed as an argument. To iterate over the callback function, we have to run the callback function n time.

[Approach 1]: Using recursion to iterate the n times callback function

  • First, create a callback function factor that takes n as an argument.
  • The factor function generates a pattern of n length.
  • Create a test function that takes a callback function and n.
  • The test function checks the value of n is equal to 0  and not.
  • If n is 0 it returns the terminate test function, else it calls the callback function which prints the pattern.
JavaScript
// callback function that print pattern
function factor(n) {
    // base case for recursion
    if (n <= 1) {
        console.log("0" + n);
        return;
    }
    // string to store patterns
    let str = "";
    // loop for generate pattern
    for (let i = 1; i <= n; i++) {
        str += `0${i} `;
    }
    // printing patterns
    console.log(str);

    // recursion call with decrement by 1
    return factor(n - 1);
}

// function to run callback function
function test(n, callback) {
    if (n == 0) {
        console.log("please provide value n greater than 0");
        return;
    }

    let k = n;
    //calling callback function
    callback(k);
}

// initialising test number
let t_number = 4;
// calling main function to call callback function
test(t_number, factor);

Output
01 02 03 04 
01 02 03 
01 02 
01

[Approach 2]: Using a loop statement to iterate over the callback. 

  • First, we create a callback function factor which generates a factorial of numbers.
  • Create a test function with argument n and a callback function.
  • Check the value of n if it is invalid terminate if not continue.
  • Create for loop with range n.
  • On each loop call the callback function which prints the factorial of each number.
JavaScript
// call back function that return factorial
function factor(number) {
    let j = 1;
    // loop that generate factorial of number
    for (let i = 1; i <= number; i++) {
        j *= i;
    }
    // printing value of factorial
    console.log(`factorial of ${number} is `);
    console.log(j);
}

// function that iterate over callback function
function test(n, callback) {
    if (n <= 0) {
        console.log("invalid number");
        return;
    }
    let k = n;
    // iterating over callback function with for loop
    for (let i = k; i >= 1; i--) callback(i);
}

// initialising test variable
let t_umber = 5;
// main function calling
test(t_umber, factor);

Output
factorial of 5 is 
120
factorial of 4 is 
24
factorial of 3 is 
6
factorial of 2 is 
2
factorial of 1 is 
1

[Approach 3]: Using Array.from() and Array.prototype.keys() for Index-Based Iteration

  • Convert the number of iterations (n) into an array of n elements using Array.from().
  • Use Array.prototype.keys() to get an iterator over the array indices.
  • Iterate over the indices to control the number of iterations.
  • Use the index in each iteration to invoke the callback function effectively.
JavaScript
// Callback function that prints the index
function printIndex(index) {
    console.log(`Iteration ${index}`);
}

// Function to iterate over callback function
function iterateCallback(n, callback) {
    if (n <= 0) {
        console.log("Invalid number of iterations");
        return;
    }

    // Create an array of size n, then iterate over its keys
    Array.from({ length: n }, (_, index) => callback(index + 1));
}

let numIterations = 5;
iterateCallback(numIterations, printIndex);

Output
Iteration 1
Iteration 2
Iteration 3
Iteration 4
Iteration 5

[Approach 4]: Using forEach() Method

The forEach() method can be used to iterate over an array. By creating an array of a specified length using Array.from(), we can use forEach() to call the callback function n times.

JavaScript
// Callback function that prints the index
function printIndex(index) {
    console.log(`Iteration ${index}`);
}

// Function to iterate over callback function
function iterateCallback(n, callback) {
    if (n <= 0) {
        console.log("Invalid number of iterations");
        return;
    }

    // Create an array of size n, then iterate over its elements
    Array.from({ length: n }).forEach((_, index) => callback(index + 1));
}

let numIterations = 5;
iterateCallback(numIterations, printIndex);

Output
Iteration 1
Iteration 2
Iteration 3
Iteration 4
Iteration 5

Syntax:

Array.from({ length: n }).forEach((_, index) => callback(index + 1));

Explore