• Function expressions, function declarations, and arrow functions
  • Default parameters and spread operators
  • Recursion with functions
  • Private variables using closures


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 ...

Get Professional JavaScript for Web Developers, 4th Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.