LearnFrenzy
Sign in Take a free test

Array Methods · Find the Error · Question 8

Error: using the result of find without a check Answer, explanation & discussion

  • Find the error
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 8 Back to Exercise

The code below stops with an uncaught error. Which statement correctly describes the problem?

main.js
const products = [{ sku: "A1", name: "Pen" }, { sku: "B2", name: "Book" }];
function nameOf(sku) {
  return products.find((p) => p.sku === sku).name;
}
console.log(nameOf("B2"));
console.log(nameOf("Z9"));
  • A It prints Book and undefined
  • B It prints Book and null
  • C It prints Book, then TypeError: find returns undefined for "Z9", and .name cannot be read from undefined Correct
  • D SyntaxError
Correct Answer: Option C

find returns undefined when nothing matches. products.find(…)?.name ?? "Unknown" handles the missing case.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links