JavaScript Objects: Object Literals, Properties & In-Memory Representation

In JavaScript, Objects are the cornerstone data structure of the entire language. An object is an unordered collection of related data and functionality stored as key-value pairs (properties and methods). Unlike primitive values, objects are stored in the Memory Heap and referenced via memory pointers.

JavaScript Object Memory Model: Stack Reference and Heap Storage

Figure 5: JavaScript Object Memory Model: Variables in Call Stack hold memory pointers to records stored in Heap.

Creating Objects & Accessing Properties

// 1. Object Literal Syntax
const course = {
    title: "Modern JavaScript Masterclass",
    durationWeeks: 8,
    "enrollment-code": "JS-2026", // Special key with hyphen requires quotes
    instructor: {
        name: "Vikram Aditya",
        experienceYears: 12
    },
    startSession() {
        return `Session for ${this.title} is now active!`;
    }
};

// 2. Dot Notation (Standard)
console.log(course.title); // "Modern JavaScript Masterclass"

// 3. Bracket Notation (Mandatory for special keys or dynamic variables)
console.log(course["enrollment-code"]); // "JS-2026"

const propertyToRead = "durationWeeks";
console.log(course[propertyToRead]);    // 8 (Dynamic lookup!)

Computed Property Names (ES6)

Allows dynamic expressions inside brackets to serve as object keys during object definition:

const keyPrefix = "student";
const studentRegistry = {
    [`${keyPrefix}_101`]: "Aarav",
    [`${keyPrefix}_102`]: "Diya"
};
console.log(studentRegistry.student_101); // "Aarav"

Adding, Modifying, and Deleting Properties

const config = { theme: "light" };

config.theme = "dark";       // Modifying existing property
config.fontSize = 16;        // Adding new property
delete config.fontSize;      // Deleting property via delete operator

// Checking property existence
console.log("theme" in config); // true
console.log(Object.hasOwn(config, "theme")); // true (ES2022 standard)

University Exam Corner (BCA Examination Notes)

Exam Question: "Explain the difference between Shallow Copy and Deep Copy of an object in JavaScript."

  • Shallow Copy: Copies top-level properties. Nested objects still share identical memory pointers (e.g. { ...original } or Object.assign({}, original)).
  • Deep Copy: Completely duplicates all nested objects recursively into independent memory addresses. Modern standard: structuredClone(original).

Next Step

How do you protect objects from being modified or extract keys/values into arrays? Proceed to: JavaScript Object Methods: Freeze, Seal & Iteration →

Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Stumbleupon Share on Tumblr Share on Reddit Share on Diggit

You may also like this!