LearnFrenzy
Sign in Take a free test

JavaScript Basics · Find the Error · Question 15

Bug: returning an object on the next line Answer, explanation & discussion

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

The code below runs without an error, but its result is wrong. Which statement correctly identifies the bug?

main.js
function makeUser(name) {
  return
  {
    name: name
  };
}
console.log(makeUser("Asha"));
  • A Object keys must be in quotes
  • B The parameter name hides the property name
  • C makeUser must be called with new
  • D ASI ends the statement after return, so the function returns undefined; put { on the same line as return Correct
Correct Answer: Option D

The braces after return are parsed as a separate block with a label (name:), which is valid code that never runs. Write return { name: name }; (or return { name };).

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