Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Stop Safely at Missing Parts

In this lesson, the year-end report ran into the parts of the club’s log that can be missing: Luis’s empty books list and the books with no review. You put ?. in a path so it stops at a missing part and gives undefined instead of a crash, and you used it before brackets and before a call too.

Key Points

  • Two parts of the log’s shape can be missing. Luis’s books array is empty, { name: 'Luis', books: [] }, and some books, such as Dev’s first book, Project Hail Mary, have no review property at all.
  • Reading a missing property gives undefined, with no error: member.books[0].review logs undefined for Dev and Hannah. Reading a property from undefined throws.
  • For Luis, member.books[0] is undefined, so the loop stops with TypeError: Cannot read properties of undefined (reading 'review'). The step before review, the first book, is the missing one.
  • A ?. in place of the plain dot, member.books[0]?.review, checks the value on its left.
  • JavaScript reads the path from left to right. If that value is null or undefined, the whole path stops there and gives undefined. Otherwise, it reads on, as a plain dot would.
  • Now the loop logs Luis undefined instead of crashing. This is called optional chaining.
  • Put the ?. right after the part that can be missing.
  • Looping over Maya’s books, every book is there, but a review may be missing, so book.review?.length gives 21 for Kindred and undefined for The Martian, which has no review.
  • Watch out: each ?. guards only the value right before it.
  • For Dev, log[3].books[0]?.review.length still throws TypeError: Cannot read properties of undefined (reading 'length').
  • His first book is there, so the ?. lets the path read on, but its review is missing, and the dot in .length is a plain one.
  • When a ?. does stop the path, nothing after it runs.
  • When the report lets you pick which detail to show, the property’s name is in a variable, and you read it with brackets. The ?. goes in front of the brackets, dot and all, and it still checks the first book.
  • In member.books[0]?.[field], the helper firstBookField(log[2], "title") gives "Parable of the Sower" for Rosa, and undefined for Luis.
  • A function can be missing too: some report pages list the books without a review, and some don’t. In printReviews(books, onMissing), the optional callback is called as onMissing?.(book).
  • With no second argument, onMissing is undefined, so the call is skipped and only the two reviews print. Pass an arrow function, and it runs for The Martian and for Educated.
  • The ?.() checks only for null or undefined. It doesn’t check that the value is a function: printReviews(log[0].books, "skip") prints Kindred’s review, then throws TypeError: onMissing is not a function when The Martian’s call runs.
  • The rule: put ?. right after a part that can be missing, and nowhere else. The log and each member’s books are always there, so leave those with plain dots, and a typo such as book for books still stops the script with an error you can read.

Final Code

The script report.js holds the club’s full log, then the guarded first-review loop, the review lengths for Maya’s books, and the two helpers. Copy it to follow along with the video:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 Your year-end report reads values from deep inside the club’s log. Before you write the next line, look at its shape again. The log is an array of members.

00:09 Each member has a name and a list of books, and each book is an object.

00:15 Two parts of this shape can be missing. Print Luis’s record, and his books list is empty. Print Dev’s first book, Project Hail Mary, and it has no review at all.

00:26 In this lesson, you’ll read through parts like these without crashing the report.

00:33 Say the report prints each member’s first review. The loop logs the member’s name, then the review of the first book in their list. Run it, and Maya, Jamal and Rosa print their reviews.

00:46 Dev and Hannah print undefined, because their first books have no review. Reading a missing property gives undefined, and that’s no error. Then, at Luis, a TypeError stops the script.

00:59 It says JavaScript couldn’t read review from undefined. The step before review is the first book, and Luis has no first book.

01:09 A missing first book is normal here, because Luis joined in December. So put a question mark and a dot right after the first book, in place of the plain dot before review.

01:20 JavaScript reads the path from left to right. At the ?., it checks the value on its left. If that value is null or undefined, the whole path stops there and gives undefined.

01:33 Otherwise, it reads on, as a plain dot would. Run it again. Maya’s path goes all the way to her review. Luis’s path stops at the missing book, and the script logs undefined instead of crashing.

01:48 This is called optional chaining.

01:52 The report also shows how long each review is. Loop over Maya’s books, and log each title with the length of its review. Here, every book is there, but a review may be missing.

02:04 So the ?. goes right after review. Your turn: pause the video and predict what this logs for The Martian.

02:17 It logs undefined. The Martian has no review, so the path stops before length. Kindred’s review is 21 characters long. Watch out: each ?. guards only the value right before it.

02:32 Dev’s first book is there, so his path reads on to review. Review is missing, and the plain dot before length throws.

02:42 The report lets you pick which detail of a first book to show. This helper takes a member and a field name, such as title, and reads it from the first book with brackets.

02:52 The ?. goes in front of the brackets, dot and all. It checks the first book, as before. For Rosa, the helper gives Parable of the Sower. For Luis, it gives undefined.

03:07 Some report pages list the books without a review, and some don’t. This helper prints each review. Its second parameter, onMissing, is an optional function it calls for a book without one.

03:19 So the call has a ?. before its parentheses.

03:23 With no second argument, onMissing is undefined, so the call is skipped. Only the two reviews print. Pass an arrow function, and it also runs for The Martian and Educated.

03:36 The ?. checks only for null or undefined, not that the value is a function. Pass a string, and Kindred prints, then The Martian’s call throws a TypeError.

03:49 Here’s the rule: put a ?. right after a part that can be missing, and nowhere else. The log and each member’s books are always there. Leave those with plain dots, so a typo like book for books still stops the script with an error you can read.

04:06 Now the report gets past Luis and every missing review, but it prints undefined in their place. Next, in Fill In Missing Values, you’ll choose what the report shows instead.

Become a Member to join the conversation.