Introduction

In this lesson, we will focus on:

  • default parameter values in functions.
  • creating dynamic HTML inside a function based on arguments.
  • handling errors with a try-catch-finally statement.

Default parameter values

To provide default values for parameters, we assign values in the function parameter list, e.g.

function printMessage(message = "No message provided") {
  // do something
}

If we call printMessage() and don’t pass in an argument to the message parameter, message will have the default value of “No message provided”.

Scrimba video

A reusable HTML function

In this video, we look at passing arguments to a function that will return different HTML based on what we pass in.

We might call this function a component. It will return a limited amount of HTML we can use elsewhere in our code.

Code from the video.

Handling errors

In the following video, we look at how to use the try-catch-finally statement to catch errors and provide feedback to the user rather than simply logging an error to the console that the user will never see.

Code from the video.

API call error handling

In this video, we will add error handling to our API call with a try-catch statement.

Code from the video.


Lesson Task

Brief

There are practice questions in the master branch of this repo.

Attempt to answer the questions before checking them against the answers branch of the repo.

Tags: