When I inspect an instance of an array (like const arr = [1, 2, 3];
), the prototype chain for that array will point to Array.prototype
.
However, I am not able to understand why Array.prototype
is displayed as an empty array (Array(0)
) in Chrome console.log()
?
I have gone through post1 and post2 but still not able to understand this basic concept.
Why [1, 2, 3]
in the console in not showing as an Array(3)
?
2
Answers
Array(0)
here refers to the [[Prototype]] value, i.e. toArray.prototype
.For instance if you log
You can see that the actual Array in the
arr
property is logged asArray(3)
, but that the[[Prototype]]
of this Array is marked as an empty Array.That’s because, like I pointed to you in your now deleted question,
Array.prototype
is actually anArray
itself. See this Q/A for more details.So if you do something like
The Array’s Prototype will now say
Array(3)
.When you inspect [1, 2, 3] in the console, you are inspecting the instance of the array, which is Array(3) because it has 3 elements.
Array.prototype, however is an empty array itself (Array(0)), and that’s what is displayed when you inspect the prototype of any array instance.
Array.prototype contains all the methods not the instance of the element.
Hope you get it, of not then do let me know.