LearnFrenzy
Sign in Take a free test

Objects · Predict the Output · Question 6

Order of spread and literal keys Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 6 Back to Exercise

What does the following JavaScript code print (console output, as in Node.js)?

main.js
const o = { a: 1, ...{ a: 2, b: 3 }, b: 4 };
console.log(o);
  • A { a: 1, b: 3 }
  • B { a: 1, b: 4 }
  • C { a: 2, b: 4 } Correct
  • D { a: 2, b: 3 }
Correct Answer: Option C

Properties are applied left to right: the spread overwrites a with 2, then the literal b: 4 overwrites b.

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