Objects and Their Internal Representation in JavaScript

Owais Qureshi
2 min readAug 10, 2023

When you think about JavaScript, you probably envision websites coming to life with interactive features and dynamic content. Behind the scenes, JavaScript relies heavily on objects for achieving these feats. Objects are the building blocks of JavaScript, allowing you to create complex structures to organize and manipulate data. In this blog, we’ll dive into the fascinating world of objects and explore their internal representation in JavaScript.

Understanding Objects: The Basics

In JavaScript, an object is like a container that holds related data and functions, known as properties and methods, respectively. Imagine an object as a real-world object — say, a car. A car has properties like color, make, and model, and methods like startEngine and brake. Similarly, a JavaScript object can store data (properties) and perform actions (methods).

Creating Objects

You can create objects in JavaScript using two main methods: object literals and constructors. Let’s look at both briefly:

1. Object Literals: This is the simplest way to create an object. You define the object’s properties and methods right when you create it.

const car = {
make: 'Toyota',
model: 'Camry',
year: 2022,
startEngine: function() {
console.log('Engine started!');
}
};

2. Constructors: Constructors are like templates for creating objects. You define a constructor function and then create new objects using the `new` keyword.

function Car(make, model, year) {
this.make = make;
this.model = model;
this.year = year;
this.startEngine = function() {
console.log('Engine started!');
};
}
const myCar = new Car('Honda', 'Civic', 2023);

Internal Representation of Objects

Now, let’s delve into the internal representation of objects in JavaScript. Internally, objects are stored as a collection of key-value pairs, where the keys are the property names and the values can be data or references to functions.

When you access an object’s property or method, JavaScript searches for the property name in the object’s internal collection of key-value pairs. If it finds a match, it returns the corresponding value.

Prototypes and Prototype Chain

Objects in JavaScript have a special property called `prototype`. This is like a blueprint that defines which properties and methods an object can inherit. When you access a property or method on an object, and it’s not found directly on the object, JavaScript looks up the prototype chain to find it.

This concept allows you to create more efficient and memory-friendly code. Instead of duplicating methods and properties for every object, you can define them in the prototype and have objects share them.

--

--