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

Time:09-21

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()

CodePudding user response:

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.

  • Related