Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why does Object.keys() return an Array of strings?

It took me quite some time to realize that Object.keys() return and Array of strings that correspond to the object keys. This is well documented (but you have to read carefully :)).

My question is: what are the (technical) reasons for the returned Array to be made of keys cast to a string? (and not the keys themselves)

In my case the keys were integers, so I had to re-cast what .keys() returns through a .map()

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

In my case the keys were integers

They were not.

Object property names can only be strings or Symbols (ECMAScript, mdn).

The numbers (JS doesn’t have an integer data type) were converted to strings when the property was created, not when the property names were read.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading