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

93
Views
Using Obejct as key inside an object

Is it possible to use object as a key inside an object?

I found the answer to be NO but with no clear explaination.
Example:

let user1 = { name: "John Doe" };
let numberOfAnswers = {}; // try to use an object

numberOfAnswers[user1] = 234; // try to use user1 object as the key
alert(numberOfAnswers) // [object][object]

But then what are we doing here? Why this seems to work? Are we not creating numberOfAnswers Object with user1 as key?

let numberOfAnswers = {
   user1: {
      visits: 123;
   }
} 
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Is it possible to use object as a key inside an object?

No. Property keys are always strings or Symbols, never objects. If you use an object as a property key, it's implicitly converted to a string (usually) or Symbol (if the object overrides the "to primitive" operation).

You can use objects as keys in Map instances, though.

But then what are we doing here? Why this seems to work? Are we not creating numberOfAnswers Object with user1 as key?

let numberOfAnswers = {
   user1: {
      visits: 123;
   }
} 

No. You're creating an object with a property whose key is the string "user1" and whose value is an object.

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!