Why an array is an object in JavaScript?

JS is a prototype-based language, so there are only primitive types and objects. It grants flexibility but makes things more confusing at the same time.

👉 Everything is an object!

Implementation of all non-primitive values in JavaScript is …


This content originally appeared on DEV Community and was authored by Nick | React tinkerer ⚛️

JS is a prototype-based language, so there are only primitive types and objects. It grants flexibility but makes things more confusing at the same time.

👉 Everything is an object!

Implementation of all non-primitive values in JavaScript is object-based.
Simply put, JavaScript has a single prototypical object from which all other objects get their initial properties. We can get it by accessing __proto__.

Object.getPrototypeOf(Object).__proto__;
Object.getPrototypeOf(Array).__proto__;
Object.getPrototypeOf(Boolean).__proto__;

// The prototypical object of every object
{
  constructor: Ć’ Object()
  hasOwnProperty: Ć’ hasOwnProperty()
  isPrototypeOf: Ć’ isPrototypeOf()
  propertyIsEnumerable: Ć’ propertyIsEnumerable()
  toLocaleString: Ć’ toLocaleString()
  toString: Ć’ toString()
  valueOf: Ć’ valueOf()
  __defineGetter__: Ć’ __defineGetter__()
  __defineSetter__: Ć’ __defineSetter__()
  __lookupGetter__: Ć’ __lookupGetter__()
  __lookupSetter__: Ć’ __lookupSetter__()
  __proto__: (...)
  get __proto__: Ć’ __proto__()
  set __proto__: Ć’ __proto__()
}

👉 Every array is an object too!

The array type is not an exception here. Array global class is a global object and an array literal is just an instance of the Array global class.
In turn, a direct prototype of the array type contains all its special methods, like fill, find, etc.

// true
Object.getPrototypeOf(Array).__proto__ === Object.getPrototypeOf(Object).__proto__
Object.getPrototypeOf([]).__proto__ === Object.getPrototypeOf(Object).__proto__


Object.getPrototypeOf([])
[
  at:  Ć’ at()
  concat:  Ć’ concat()
  constructor:  Ć’ Array()
  copyWithin:  Ć’ copyWithin()
  entries:  Ć’ entries()
  every:  Ć’ every()
  fill:  Ć’ fill()
  filter:  Ć’ filter()
  find:  Ć’ find()
  findIndex:  Ć’ findIndex()
  findLast:  Ć’ findLast()
  findLastIndex:  Ć’ findLastIndex()
  flat:  Ć’ flat()
  ...
]

👉 How is it implemented in the JavaScript engine?

Similarly, arrays are a special case of objects inside the JavaScript engine.
But they have:

  • special handling of indices
  • a magical length property

To understand how objects work, check out my article.

👉 Indices handling

Array indices are represented as strings, that contain numbers.
So every element inside an array is associated with a numeric string.

indices handling

👉 Length property

The length is just non-configurable and non-enumerable property. The JavaScript engine automatically updates its value once an element is added to the array or deleted from it.

length property

P.S. Follow me on Twitter for more content like this!


This content originally appeared on DEV Community and was authored by Nick | React tinkerer ⚛️


Print Share Comment Cite Upload Translate Updates
APA

Nick | React tinkerer ⚛️ | Sciencx (2022-02-08T21:57:50+00:00) Why an array is an object in JavaScript?. Retrieved from https://www.scien.cx/2022/02/08/why-an-array-is-an-object-in-javascript/

MLA
" » Why an array is an object in JavaScript?." Nick | React tinkerer ⚛️ | Sciencx - Tuesday February 8, 2022, https://www.scien.cx/2022/02/08/why-an-array-is-an-object-in-javascript/
HARVARD
Nick | React tinkerer ⚛️ | Sciencx Tuesday February 8, 2022 » Why an array is an object in JavaScript?., viewed ,<https://www.scien.cx/2022/02/08/why-an-array-is-an-object-in-javascript/>
VANCOUVER
Nick | React tinkerer ⚛️ | Sciencx - » Why an array is an object in JavaScript?. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/02/08/why-an-array-is-an-object-in-javascript/
CHICAGO
" » Why an array is an object in JavaScript?." Nick | React tinkerer ⚛️ | Sciencx - Accessed . https://www.scien.cx/2022/02/08/why-an-array-is-an-object-in-javascript/
IEEE
" » Why an array is an object in JavaScript?." Nick | React tinkerer ⚛️ | Sciencx [Online]. Available: https://www.scien.cx/2022/02/08/why-an-array-is-an-object-in-javascript/. [Accessed: ]
rf:citation
» Why an array is an object in JavaScript? | Nick | React tinkerer ⚛️ | Sciencx | https://www.scien.cx/2022/02/08/why-an-array-is-an-object-in-javascript/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.