10
Functions
WHAT'S IN THIS CHAPTER?
- Function expressions, function declarations, and arrow functions
- Default parameters and spread operators
- Recursion with functions
- Private variables using closures
WROX.COM DOWNLOADS FOR THIS CHAPTER
Please note that all the code examples for this chapter are available as a part of this chapter's code download on the book's website at www.wrox.com/go/projavascript4e
on the Download Code tab.
Some of the most interesting parts of ECMAScript are its functions, primarily because functions actually are objects. Each function is an instance of the Function
type that has properties and methods just like any other reference type. Because functions are objects, function names are simply pointers to function objects and are not necessarily tied to the function itself. Functions are typically defined using function-declaration syntax, as in this example:
function sum (num1, num2) {
return num1 + num2;
}
In this code, a variable sum
is...