LearnFrenzy
Sign in Take a free test

Operators · Find the Error · Question 6

Error: the in operator on a string Answer, explanation & discussion

  • Find the error
  • Hard
  • 0 comments
  • Answer explained
  • Moderated community
Question 6 Back to Exercise

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

main.js
const record = "Asha";            // expected an object like { name: "Asha" }
if ("name" in record) {
  console.log("Has a name");
}
  • A TypeError: in can only search an object – Cannot use 'in' operator to search for 'name' in Asha Correct
  • B It prints Has a name
  • C Nothing is printed
  • D SyntaxError
Correct Answer: Option A

The right operand of in must be an object; primitives are not wrapped for in. Check the type first (typeof record === "object" && record !== null).

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