I understand the notion of passing a function as a variable in javascript - whether it be a function variable or an anonymous function. However, I don't understand when or why a function should be executed as a parameter instead of before or after the primary function call.
For example:
Assuming a callback function and a private function are created -
function callback(y) {
alert(y);
}
function foo(x, callback) {
alert(x);
}
what's the difference between these two statements and when might the latter one be more practical than the first?
foo(bar);
callback(bas);
or
foo(bar, callback(bas));
I'm sure this isn't syntactically correct, but hopefully it illustrates what I'm trying to convey.
Ultimately, I'm trying to understand when running a function as a parameter is practical.