Question 8
Back to Exercise
The code below stops with an uncaught error. Which statement correctly describes the problem?
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!