Javascript - Functions as Values


Javascript function names are variables, functions can be used any place value can be used.

You can pass a function into another function as an argument and return a function from another function.

Consider the following function:

function callSomeFunction(someFunction, someArgument){
    return someFunction(someArgument);

This function accepts two arguments. The first argument should be a function, and the second argument should be a value to pass to that function.

Any function can then be passed in as follows:

function add10(num){
    return num + 10;

var result1 = callSomeFunction(add10, 10);
console.log(result1);   //20

function getGreeting(name){
    return "Hello, " + name;

var result2 = callSomeFunction(getGreeting, "First");
console.log(result2);   //"Hello, First"

The callSomeFunction() function is generic.