Can't force object keys to be integer

Viewed 1423
const obj = {
15: 100
};
for(let key in obj)
    console.log(key, typeof(key), typeof(+key))

The result is 15 string number. I'm trying to iterate over object values and put some of them into Map object but types compatibility seems unable to achieve. Am I doing something wrong here or object keys are always strings?

Object.keys(obj)

also returns ["15"]

1 Answers

Object keys are always strings. You can see more about it here:

Property names must be strings. This means that non-string objects cannot be used as keys in the object. Any non-string object, including a number, is typecasted into a string via the toString method.

For you to be able to achieve what you want you will need to cast the keys back to integers.

Related