notFound
notFound

Reputation: 50

How can we pass argument to function which itself is passed as argument in javascript

function fun (a, b)  {
        console.log(a + ' ' + b);
        return a == b;
 }

function test(condition) {

    for(let i = 0; i < 1; i++) {
        if(condition()){
            break;
        }
    }
}

How can we pass function fun as an argument inside function test with the argument a and b (a and b are variable). writing test(fun('a', 'b')); is resulting in an error.

Upvotes: 0

Views: 81

Answers (1)

Kien Nguyen
Kien Nguyen

Reputation: 2691

You can use a closure like this:

test(function(){ return fun('a', 'b'); });

Upvotes: 1

Related Questions