JagsSparrow JagsSparrow - 10 months ago 29
Javascript Question

Why Object.keys is returns array of string instead of array of Numbers

When I run following code

var obj = { 0: 'a', 1: 'b', 2: 'c' };
typeof Object.keys(obj)[0] //returns string

object i'm creating Number keys.

Any reason, why its string and not a


Because Object.keys returns an array with strings

Object.keys() returns an array whose elements are strings corresponding to the enumerable properties found directly upon object. The ordering of the properties is the same as that given by looping over the properties of the object manually.

You get an array of strings, because Property names are strings by definition.

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