In JavaScript functions may be used as regular values, as well. That means that they may be passed to other functions as arguments and be returned from other functions as values.

Observe following example:

function functionOne(functionTwo, argument) {

   return functionTwo(argument);

}

With some real number it might look like this:

function getPersonName() {

   return "Joe";

}

function saluteName(name) {

   return "Hi " + name;

}

function personName(name) {

   return saluteName(name);

}

var addname = personName(getPersonName);

alert (addname); // "Hi Joe"

For the purpose of learning both cases, function as argument and function as value, are visualized in the example below.

Example

Example of function returned as a value:

 

›› go to examples ››