Prepare for the Salesforce JavaScript Developer Exam. Utilize comprehensive quizzes, flashcards, and multiple choice questions with hints and explanations. Boost your exam readiness!

Practice this question and more.


What is the output of the following console log for an object with duplicate keys?

  1. { a: "one", b: "two" }

  2. { b: "two", a: "three" }

  3. { a: "three", b: "two" }

  4. SyntaxError

The correct answer is: { a: "three", b: "two" }

When an object is defined in JavaScript with duplicate keys, the last occurrence of the key will override any previous values assigned to that key. This means that in cases where the same key appears multiple times, you only end up with the last assigned value. In the scenario being considered, if we have an object like this: ```javascript const example = { a: "one", b: "two", a: "three" }; ``` The key `a` is present twice, with the first value being "one" and the second value being "three". Upon evaluation, JavaScript will ignore the first declaration of `a` and keep the last one, resulting in the object effectively being: ```javascript { a: "three", b: "two" } ``` Therefore, the output of the console log will indeed reflect that the key `a` has the value "three" while the key `b` retains the value "two". This aligns with the output described in the correct choice. It is important to note that while you can define objects with duplicate keys, this practice can lead to confusion and bugs, so it is generally advisable to ensure all object keys are unique.