DEV Community

Cover image for Guess the JavaScript Output: Very Tricky!
Sadeedpv🥇
Sadeedpv🥇

Posted on

Guess the JavaScript Output: Very Tricky!

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]);
Enter fullscreen mode Exit fullscreen mode

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)

Collapse
 
blackr1234 profile image
blackr1234

Guess the object should be casted to something. The values of b and c should result in the same value. So would guess 456 as the element gets overwritten. a[c] should give 456 too.

Collapse
 
rahulbenzeen profile image
RahulBenzeen

456

Collapse
 
bob4262 profile image
Bob dnaeil

123

Collapse
 
akashakki profile image
Akash Bais

456

Collapse
 
jonrandy profile image
Jon Randy 🎖️

456

Collapse
 
jonrandy profile image
Jon Randy 🎖️

I won't write why as it makes it too easy for everyone else :)