Functions as Values
JavaScript functions can be used any place other value can be used.
<!DOCTYPE html>
<html>
<head>
<title>Example</title>
<script type="text/javascript">
function callFunction(aFunction, anArgument){
return aFunction(anArgument);
}
function add10(num){
return num + 10;
}
var result1 = callFunction(add10, 10);
document.writeln(result1); //20
function getGreeting(name){
return "Hello, " + name;
}
var result2 = callFunction(getGreeting, "JavaScript");
document.writeln(result2); //"Hello, JavaScript"
</script>
</head>
<body>
</body>
</html>
Home
JavaScript Book
Essential Types
JavaScript Book
Essential Types
Function:
- The Function Type
- Function Declarations versus Function Expressions
- Functions as Values
- Returning a function from a function
- Function arguments
- this for function context
- Function caller
- Function length property
- Function apply()
- Function.call() method
- Function's bind() method
- Function toLocaleString() and toString()