This post is part of a series called Guess the JavaScript Output
And today's question is a bit tricky one:
What will be the output of the following code?
let a = {};
let b = { key: "b" };
let c = { key: "c" };
a[b] = 123;
a[c] = 456;
console.log(a[b]);
Uncle Mike solved this within 20s (Mike started coding in the womb and has built his own OS called MikeOS).
If you'd like to see more of the series, please give it a like.
Top comments (6)
Guess the object should be casted to something. The values of
b
andc
should result in the same value. So would guess456
as the element gets overwritten.a[c]
should give456
too.456
123
456
456
I won't write why as it makes it too easy for everyone else :)