Search code examples
javascriptpropertiesprototype

Why Array's have a given property length, why it's not in prototype?


Hello I've learned that inheritance happens with prototypes. What I don't understand is, let's say: we create a simple array

const arr = [2, 4];

Now, why is Array.hasOwnProperty('length'); returns true here. The length property should be inherited therefore it should be in the prototype not in its own properties. Why length is a exception here?


Solution

  • Every array has its own length property because every array could have a different length. Even though all arrays have one, it's not an inherited method (because it's not a method at all), and it's not an inherited getter/setter either, it's a magic property created during construction of the array instance.