Chapter 4: Functions

A function is a chunk of code that can be referenced by a name, and is almost like a small, self-contained mini program. Functions can help reduce repetition and make code easier to follow.

In this chapter, we’ll be covering these topics:

  • Defining functions―function declarations, function expressions, Function() constructors and the new arrow syntax

  • Invoking a function

  • Return values

  • Parameters and arguments

  • Hoisting―variables and functions

  • Callbacks―functions as a parameter

  • Project ― we’ll be using functions to make the Quiz Ninja code easier to follow

In JavaScript, functions are considered to be first-class objects. This means they behave in the same way as all the other primitive data types and objects in the ...

Get JavaScript: Novice to Ninja, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.