Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

109
Views
Can we use object as a key of an object in Javascript?

I was given a short javascript code in an interview to give the output of that but I was not sure what will be the output of that. He creates a var object and assigns objects as indexes of that object using the array data structure. I did console that code after the interview but still do not understand how it is showing the output.

Here is the code

var a = {};
b = { key: 'b'};
c = { key: 'c'};
a[b] = 123;
a[c] = 456;

console.log(a[b]); //what is the output of this console statement and explain why

Can anyone explain with javascript logic behind the output it shows? Thanks in advance!

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

The object used as key's toString is [object Object] and that is what is used each time

var a = {};
b = { key: 'b'};
c = { key: 'c'};
a[b] = 123; // sets a["[object Object]"]
console.log(b,a[b])
a[c] = 456; // ALSO sets a["[object Object]"]
console.log(b,a[b])
console.log(Object.keys(a))
console.log(a[b]);

console.log(a["[object Object]"]);

about 4 years ago · Juan Pablo Isaza Report

0

You could have a look to the object a, where you see the stringed object (with toString())

[object Object]

as accessor.

Objects can have only strings as properties and with the latest Symbol, it could be this one, too.

var a = {};
b = { key: 'b'};
c = { key: 'c'};
a[b] = 123;
a[c] = 456;

console.log(a[b]); //what is the output of this console statement and explain why
console.log(a);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!