LearnFrenzy
Sign in Take a free test

Loops · Find the Error · Question 7

Error: for…of over null Answer, explanation & discussion

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

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

main.js
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!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links