Question 7
Back to Exercise
The code below stops with an uncaught error. Which statement correctly describes the problem?
function printTags(post) {
for (const tag of post.tags) console.log("#" + tag);
}
printTags({ title: "Hello", tags: null });
- A TypeError: post.tags is null and null is not iterable – use post.tags ?? [] Correct
- B Nothing is printed
- C It prints #null
- D SyntaxError
Correct Answer: Option A
for…of needs an iterable. A safe default, for (const tag of post.tags ?? []), loops zero times when tags are missing.
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!