10Functions
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 defined and initialized to be a function. Note that there is no name included after the function keyword because it's not needed—the function can be referenced by the variable sum. Also note that there is no semicolon after the end of the function definition.
The function-declaration syntax is almost exactly equivalent to using a function expression, such as this:
let sum = function(num1, num2) {return num1 + num2;};
Note that there is a semicolon after the end of the function, just as there would be after ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access