CHAPTER 9 · STRUCTURED DATA

JavaScript Objects

Objects related data ko key-value pairs me organize karte hain. Is chapter me properties, methods, nested objects, this, destructuring, spread aur practical object patterns step by step samjhenge.

English + Hinglish40 min readPractice included

What is an object?

An object is a collection of related values stored as properties. Each property has a key and a value.

object.jsJS
const student = {
  name: "Aman",
  course: "JavaScript",
  score: 88
};

console.log(student);
Hinglish Explanation

Object ko ek labeled box samjho. Array me values numbered positions par hoti hain; object me values meaningful keys jaise name, course aur score ke saath store hoti hain.

Creating object literals

The most common beginner syntax is an object literal using curly braces.

create.jsJS
const course = {
  title: "JavaScript",
  level: "Beginner",
  isLive: true
};

Property values can be strings, numbers, booleans, arrays, functions, other objects and more.

Dot notation and bracket notation

Use dot notation for simple known property names. Bracket notation is useful for dynamic keys or keys that are not valid identifier names.

access.jsJS
const student = { name: "Riya", "course-level": "Beginner" };

console.log(student.name);
console.log(student["course-level"]);

const key = "name";
console.log(student[key]);
Remember: student.key looks for a property literally named key. For a variable-based key, use student[key].

Add, update and delete properties

Object properties can be added or updated after creation. delete removes an own property.

update.jsJS
const profile = { name: "Neha" };
profile.city = "Delhi";
profile.name = "Neha Sharma";
delete profile.city;

console.log(profile);

A const object binding cannot be reassigned, but properties of the existing object can still change.

Object methods

A property can store a function. When a function belongs to an object, it is commonly called a method.

method.jsJS
const learner = {
  name: "Aman",
  greet() {
    return "Hello, " + this.name;
  }
};

console.log(learner.greet());

Understanding this in methods

In a normal method call such as learner.greet(), this usually refers to the object used to call the method.

this.jsJS
const course = {
  title: "JavaScript",
  showTitle() {
    console.log(this.title);
  }
};

course.showTitle();
Arrow function caution: Arrow functions do not create their own this. Do not automatically replace normal object methods with arrows when you need method-style this.

Nested objects

Objects can contain other objects, which is useful for structured data.

nested.jsJS
const student = {
  name: "Riya",
  progress: {
    html: 100,
    css: 90,
    javascript: 55
  }
};

console.log(student.progress.javascript); // 55

Arrays of objects

Real applications often store multiple records as an array of objects.

records.jsJS
const students = [
  { name: "Aman", score: 82 },
  { name: "Riya", score: 91 },
  { name: "Neha", score: 76 }
];

const passed = students.filter(student => student.score >= 80);
console.log(passed);
Real-world pattern

Array multiple records ko hold karta hai aur har object ek record ki details rakhta hai. Ye API data aur dashboards me bahut common pattern hai.

Object destructuring

Destructuring lets you extract properties into variables.

destructure.jsJS
const student = { name: "Aman", score: 88 };
const { name, score } = student;

console.log(name, score);

Rename and default values

Destructuring can rename a property and provide a default when a property is undefined.

destructure-advanced.jsJS
const user = { name: "Riya" };
const { name: userName, role = "student" } = user;

console.log(userName); // Riya
console.log(role);     // student

Object spread

Spread syntax creates a shallow copy and can combine or override properties.

spread.jsJS
const base = { name: "Aman", score: 70 };
const updated = { ...base, score: 85, active: true };

console.log(updated);
Order matters: Later properties overwrite earlier properties with the same key.

Rest properties

Rest syntax can collect the remaining properties during destructuring.

rest.jsJS
const student = { name: "Neha", score: 92, city: "Lucknow" };
const { name, ...details } = student;

console.log(name);
console.log(details);

Object.keys(), values() and entries()

These methods return arrays of an object's own enumerable string-keyed properties.

object-methods.jsJS
const course = { title: "JS", level: "Beginner", lessons: 18 };

console.log(Object.keys(course));
console.log(Object.values(course));
console.log(Object.entries(course));

Looping through object entries

Object.entries() works well with for...of.

loop-object.jsJS
const progress = { html: 100, css: 90, js: 60 };

for (const [topic, value] of Object.entries(progress)) {
  console.log(topic, value);
}

Checking whether a property exists

Object.hasOwn() checks whether an object directly owns a property.

has-own.jsJS
const profile = { name: "Aman" };

console.log(Object.hasOwn(profile, "name")); // true
console.log(Object.hasOwn(profile, "role")); // false

Computed property names

Bracket syntax inside an object literal can use an expression as a property key.

computed.jsJS
const key = "score";
const student = {
  name: "Riya",
  [key]: 95
};

console.log(student.score);

Objects are reference values

Assigning one object variable to another does not clone the object. Both bindings can refer to the same object.

reference.jsJS
const original = { score: 70 };
const same = original;
same.score = 90;

console.log(original.score); // 90

Spread creates a shallow copy, not a deep clone of nested objects.

shallow.jsJS
const original = { progress: { js: 50 } };
const copy = { ...original };
copy.progress.js = 80;

console.log(original.progress.js); // 80
Shallow means one level: Top-level object naya hota hai, lekin nested reference abhi bhi share ho sakta hai.

Optional chaining and nullish fallback

Optional chaining safely stops when an intermediate value is null or undefined. Nullish coalescing can provide a fallback.

optional.jsJS
const student = { profile: { name: "Aman" } };

const city = student.profile?.address?.city ?? "Not added";
console.log(city);

Beginner best practices

  • Property names meaningful rakho.
  • Known simple key ke liye dot notation, dynamic key ke liye bracket notation use karo.
  • Method me this chahiye ho to arrow function blindly use mat karo.
  • Nested structures ko unnecessarily deep mat banao.
  • Updates me object spread readable ho sakta hai, but shallow-copy behavior samjho.
  • Missing nested data ke liye optional chaining useful hai.
  • Object methods ko ek clear responsibility do.

Common beginner mistakes

  • student.key ko variable-based dynamic access samajhna.
  • const object ki properties immutable samajhna.
  • Object method ke this ko har context me same assume karna.
  • Direct assignment ko object clone samajhna.
  • Spread copy ko deep clone samajhna.
  • Nested missing property ko direct access karke error create karna.
  • Array aur object ko same access pattern se use karna.
  • Later spread properties previous values overwrite karti hain ye bhoolna.

Chapter checklist

  • Object literal create kar sakte ho?
  • Dot aur bracket notation ka difference clear hai?
  • Property add, update aur delete kar sakte ho?
  • Method aur this ka beginner-level role samajh aaya?
  • Nested objects aur arrays of objects read kar sakte ho?
  • Destructuring, spread aur rest syntax recognize kar sakte ho?
  • Object.keys/values/entries use kar sakte ho?
  • Reference vs shallow copy ka difference clear hai?

Practice Task — Student Profile Object

Browser Console me ek structured student profile banao.

  1. name, course, score aur isActive properties wala object banao.
  2. Dot notation se name aur bracket notation se course read karo.
  3. city property add karo aur score update karo.
  4. Ek getResult() method banao jo score ke basis par Pass/Fail return kare.
  5. progress naam ka nested object add karo.
  6. Object destructuring se name aur score nikalo.
  7. Spread se object ka updated shallow copy banao.
  8. Object.keys() aur Object.entries() ka output dekho.
  9. Object.entries() ke saath for...of loop run karo.
  10. Optional chaining se missing nested property safely read karo.
  11. Direct assignment aur spread copy ka reference behavior compare karo.