Objects and their internal representation in javascript

Vamsi K
Nov 8, 2020

Objects:

Objects in JavaScript, just as in many other programming languages, can be compared to objects in real life. The concept of objects in JavaScript can be understood with real life, tangible objects.

In JavaScript, an object is a standalone entity, with properties and type. Compare it with a cup, for example. A user is an object, with properties. A user has a name, age, mobile, etc. In the same way, JavaScript objects can have properties, which define their characteristics.

user object image
user object

The internal/memory representation of objects:

The majority of the objects contain all their properties in a single block of memory and all those memory blocks have a pointer to a map, which describes their structure.
Named properties that don’t fit in an object are usually stored in an overflowing array.
Numbered properties are stored separately, usually in a contiguous array.

Javascript allows flexibility in declaring the objects, this creates confusion in giving a solid representation of the object. An object is a collection of properties: basically key-value pairs. You can access properties using two different kinds of expressions:

  • obj.prop
  • obj[“prop”]

Note: If you use a number as a property name, it gets converted to a string. This allows Objects to store values of negative or fractional array indices(which eventually gets converted into strings).

--

--