0

I was stumbling around in JavaScript and was wondering why my tests were still working, although I changed a constant from an Object to a Variable.

I'm aware that Object properties can be accessed by key-values, e.g.

const object = {};
object['keyValue'] = 'property';

// creates this object
{ keyValue: 'property' };

I thought that this was only possible for Objects, however, I accidentally executed the following code:

const array = [];
array['maybeKeyValue'] = { keyValue: 'property'};

// this created the following array
[ship: {keyValue: 'property'}]

// i can also access the object in the array
const obj = array['ship'];
// returns the created object

I also noticed that the array.length is 0 and that array.entries() does not create an valid iterator, which is strange for me. What is happening here? Is this a valid alternative to storing keys with values in an Object?

Marten
  • 645
  • 7
  • 21

0 Answers0