LearnFrenzy
Sign in Take a free test

Objects · Predict the Output · Question 18

Computed keys with side effects Answer, explanation & discussion

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

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

main.js
let i = 0;
const o = { [`item${++i}`]: i, [`item${++i}`]: i };
console.log(o);
  • A { item1: 0, item2: 1 }
  • B { item2: 2 }
  • C { item1: 2, item2: 2 }
  • D { item1: 1, item2: 2 } Correct
Correct Answer: Option D

Each key expression is evaluated before its value, left to right: ++i makes i 1 for the first key, so its value is 1; then 2 for the second.

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