Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

111
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda