LearnFrenzy
Sign in Take a free test

Conditional Statements · Find the Error · Question 9

Error: switch on a property of null Answer, explanation & discussion

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

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

main.js
const order = null;                       // order not found
switch (order.status) {
  case "shipped": console.log("On the way"); break;
  default: console.log("Processing");
}
  • A It prints Processing
  • B TypeError: order is null, so order.status cannot be read before the switch even starts Correct
  • C It prints On the way
  • D Nothing is printed
Correct Answer: Option B

The switch expression is evaluated first and throws. Guard it: if (!order) return …; or switch (order?.status).

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