Could you use Javascript objects as keys to another object?

No, you can’t. I’ve got an example for you:

var a={},

Output of both console.log statements will be 456. To understand why it it so, we should use base Object method toString() on both objects. We will see following output:

> b.toString();
< "[object Object]"
> c.toString();
< "[object Object]"

So when you use object as a key of another object it internally converts to string with value [object Object]. So this is all equal calls:

a[b] === a[c] === a[{}] === a["[object Object]"]

But if you still want to use object as a key, you should use ES6 Maps.

Written by

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store