Improve Question 25 Explanation
As far as I was aware, object order isn't guaranteed and just a side-effect of specific browser implementations. This seems to have changed.
Since this will be a very common misconception, I think it should be mentioned in the answer (preferably including a source).
The only thing that ES6 changes over ES5 is this (from reading the article):
All properties that are integer indices appear first in the overall object property order and are sorted numerically.
I think it is still murky, I don't think anything in the spec guarantees an order for when the object is printed through console.log so I still think that the answer is misleading. The answer can be B and C. Though typically with current implementations you'll see C but nothing preventing it from being B in an implementation.
After thinking this is a non-issue I must admit this is an actual issue :sweat_smile:. So the interesting part is:
const til = { a: "one", '1337': "two", a: "three" };
console.log(til);
According to the explanation of the current question this will result in:
{ a: "three", 1337: "two" }
While it actually logs (mentioned by @FlorianWendelborn)
{ 1337: "two", a: "three" }
So I'm not entirely sure how you want to fix this, change the explanation or change the question entirely.
I'd just switch it with a different question. There is no value here, and I don't think anyone would like to see this behaviour used meaningfully in production code. One of the rougher edges of the language.
I still think this is advanced EcmaScript knowledge so I would like this answer to stay. This is one idea to use the quircky integer indices rule:
25. What's the output?
const obj = { 3: "zero", 1: "two", 3: "four" };
console.log(obj);
- A:
{ 3: "zero", 1: "two" } - B:
{ 1: "two", 3: "zero" } - C:
{ 1: "two", 3: "four" } - D:
{ 3: "four", 1: "two" } - E:
SyntaxError
Answer
Answer: C
If you have two keys with the same name, the key will be replaced. It will still be in its first position, but with the last specified value. All properties that are integer indices appear first in the overall object property order and are sorted numerically, the other properties are in chronological order.
But then better demonstrated by including both integer and string keys,
> const obj = { a: "one", b: "two", a: "three" , 3: "four", 1: "five", 3: "six" };
> console.log(obj);
{1: "five", 3: "six", a: "three", b: "two"}
thus showing both string keys in "order of key insertion" and integer keys placed first and sorted in "numerical order", with the latest value set by a key instance overwriting earlier values.
(Is this when the Python folk run screaming?)