Fill In Missing Values

In the last lesson, ?. got the year-end report past Luis’s empty list and every missing review. Where a value was missing, though, the report printed undefined, and readers shouldn’t see that. They should see a placeholder like “(no review)” or a rating that tells the truth.

In this lesson, you’ll choose a fallback for each missing value and fill defaults into a copy of a book record. You’ll also combine two fallback operators without a SyntaxError and decide which values shouldn’t get a fallback at all. Start with Dev, the one member whose books have a missing review and a rating of 0.

Choose What Counts as Missing

A report line needs something to print in every slot, but not every book has every value. Here’s the shape of Dev’s record, with only the properties this lesson reads:

dev: object { name, books }
  books: array of 2 book objects
    [0] { title: "Project Hail Mary", rating: 4 }
    [1] { title: "Circe", rating: 0, review: "Gave up halfway." }

Project Hail Mary has no review key at all. Circe has a review and a rating of 0 because Dev abandoned it halfway.

To keep the runs short, create a new file, report-line.js, with a copy of Dev’s record from report.js. Then add a loop that prints one line per book. A missing review should read “(no review)”, and the || operator gives you that in one expression. The rating gets the same treatment, in case it’s missing:

Language: JavaScript Filename: report-line.js
const dev = {
  name: "Dev",
  books: [
    {
      title: "Project Hail Mary",
      author: "Andy Weir",
      genre: "sci-fi",
      rating: 4,
    },
    {
      title: "Circe",
      author: "Madeline Miller",
      genre: "fantasy",
      rating: 0,
      review: "Gave up halfway.",
    },
  ],
};

for (const book of dev.books) {
  const rating = book.rating || "unrated";
  const review = book.review || "(no review)";
  console.log(`${book.title}: ${rating}, ${review}`);
}

Before you run it, predict the line for Circe. Then run the script and compare:

Circe’s line reads “unrated”:

Language: Shell
$ node report-line.js
Project Hail Mary: 4, (no review)
Circe: unrated, Gave up halfway.

The review fallback worked, but Dev did rate Circe, with a 0. The || operator gives back its right side whenever its left side is falsy, and 0 is falsy. That value also slips past an if check, because a decision treats every falsy value as false.

The fix is the operator you’ve used since the genre tally. The ?? operator gives back its right side only when the left side is null or undefined. To see where the two operators part ways, try both on the values a book could hold, in Node’s REPL:

Language: JavaScript
> 0 || "unrated"
'unrated'
> 0 ?? "unrated"
0
> "" || "(no review)"
'(no review)'
> "" ?? "(no review)"
''
> false || "unrated"
'unrated'
> false ?? "unrated"
false
> null ?? "unrated"
'unrated'
> undefined ?? "(no review)"
'(no review)'

For null and undefined, both operators fall back. For 0, "", and false, only || does. So the choice depends on what counts as missing for each value.

A rating of 0 is a real opinion, so the rating line needs ??. A review is different. If a member cleared the review box and left "", there’s nothing to print, so || is the right choice.

Change the rating line in report-line.js to const rating = book.rating ?? "unrated";, and Circe keeps its 0.

If you’re not sure which operator suits another property of the records, talk it through with the mentor:

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

Become a Member to join the conversation.