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
booksarray is empty,{ name: 'Luis', books: [] }, and some books, such as Dev’s first book, Project Hail Mary, have noreviewproperty at all. - Reading a missing property gives
undefined, with no error:member.books[0].reviewlogsundefinedfor Dev and Hannah. Reading a property fromundefinedthrows. - For Luis,
member.books[0]isundefined, so the loop stops withTypeError: Cannot read properties of undefined (reading 'review'). The step beforereview, 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
nullorundefined, the whole path stops there and givesundefined. Otherwise, it reads on, as a plain dot would. - Now the loop logs
Luis undefinedinstead 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?.lengthgives21for Kindred andundefinedfor The Martian, which has no review. - Watch out: each
?.guards only the value right before it. - For Dev,
log[3].books[0]?.review.lengthstill throwsTypeError: Cannot read properties of undefined (reading 'length'). - His first book is there, so the
?.lets the path read on, but itsreviewis missing, and the dot in.lengthis 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 helperfirstBookField(log[2], "title")gives"Parable of the Sower"for Rosa, andundefinedfor 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 asonMissing?.(book). - With no second argument,
onMissingisundefined, 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 fornullorundefined. It doesn’t check that the value is a function:printReviews(log[0].books, "skip")prints Kindred’s review, then throwsTypeError: onMissing is not a functionwhen The Martian’s call runs. - The rule: put
?.right after a part that can be missing, and nowhere else. The log and each member’sbooksare always there, so leave those with plain dots, and a typo such asbookforbooksstill 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: