Functions in JavaScript

Functions = how to name a piece of code and reuse it.

This chapter has 2 lessons and 8 exercises you run and check in your browser. Plan on about 34 minutes.

  • Function Basics: declaring and calling functions, parameters, return and hoisting.
  • Types of Functions: arrow functions, callbacks, higher-order, recursive and async functions.

After this chapter, you can:

  • Wrap repeated code in a function and call it by name.
  • Pass values in with parameters and get an answer back with return.
  • Write short arrow functions and pass functions as callbacks.
  • Solve a problem with recursion, using a base case to stop.

Every lesson has short explanations, code boxes you edit and run in the live editor, and a web page example you can click.

Before you start, finish Basics.

Next chapter: Dates and Times.

Lessons in this section

  1. Function Basics

    What a function is and why it helps, declaring and calling functions, parameters, arguments and default values, return versus console.log, and function hoisting.

    • Explain what a function is and why it removes repeated code
    • Write a function declaration and call it
    • Tell parameters apart from arguments
    • Give a parameter a default value
  2. Types of Functions

    Named, anonymous, expression and arrow functions, callbacks and higher-order functions, nested and recursive functions with a base case, and async functions.

    • Tell a named function from an anonymous one
    • Store a function in a variable as a function expression
    • Write an arrow function with a concise body
    • Pass a callback into another function