DEV Community

Cover image for 🧠 10-Day JS Challenge: Objects & Nested Data Day-7
Smriti Singh
Smriti Singh

Posted on

🧠 10-Day JS Challenge: Objects & Nested Data Day-7

πŸ“… Day 7: Objects & Nested Data

Welcome to Day 7 of the challenge!

Today we're diving into objectsβ€”a key data structure in JavaScript that helps us store data in the form of key-value pairs. Objects are perfect for representing real-world entities like users, products, or configurations.

🧩 What is an Object?
An object is a collection of related data and functions (called properties and methods). Each property is a key-value pair.

let user = {
  name: "Smriti",
  age: 24,
  isMember: true
};
Enter fullscreen mode Exit fullscreen mode

πŸ” Accessing Object Properties
You can access object properties in two ways:

1. Dot Notation

console.log(user.name); // Smriti
Enter fullscreen mode Exit fullscreen mode

2. Bracket Notation

console.log(user["age"]); // 24
Enter fullscreen mode Exit fullscreen mode

Bracket notation is useful when:

  • The key is stored in a variable
  • The key has spaces or special characters

🧱 Updating & Adding Properties

user.name = "Aarav";          // Update
user.email = "aarav@email.com"; // Add
Enter fullscreen mode Exit fullscreen mode

You can also delete properties:

delete user.isMember;
Enter fullscreen mode Exit fullscreen mode

🧭 Nested Objects and Arrays
Objects can contain other objects and arrays β€” making it easy to model complex data.

let student = {
  name: "Ananya",
  age: 18,
  subjects: ["Math", "Science", "English"],
  address: {
    city: "Delhi",
    pin: 110001
  }
};

console.log(student.subjects[1]);       // "Science"
console.log(student.address.city);      // "Delhi"
Enter fullscreen mode Exit fullscreen mode

❄️ Using Object.freeze() to Lock an Object
If you want to make an object read-only, use Object.freeze().

const settings = {
  theme: "dark",
  fontSize: 16
};

Object.freeze(settings);

settings.theme = "light"; // Ignored!
console.log(settings.theme); // "dark"
Enter fullscreen mode Exit fullscreen mode

Note: Object.freeze() only works at the top level β€” nested objects remain mutable unless you recursively freeze them.

βœ… Mini Task: Create and Print a Student Object
🎯 Task:
Create a student object with:

  • name
  • age
  • subjects (an array)
  • address (nested object with city and pin)
  • Print the information neatly.

πŸ’» Example:

const student = {
  name: "Aarav",
  age: 20,
  subjects: ["Math", "Physics", "Chemistry"],
  address: {
    city: "Mumbai",
    pin: 400001
  }
};

Object.freeze(student);

console.log("Name:", student.name);
console.log("Age:", student.age);
console.log("Subjects:", student.subjects.join(", "));
console.log("City:", student.address.city);
console.log("PIN Code:", student.address.pin);
Enter fullscreen mode Exit fullscreen mode

❓ Interview Questions (Day 7 Topics)

  1. What is the difference between dot notation and bracket notation?
  2. How do you add or update properties in an object?
  3. How can you represent nested data in JavaScript?
  4. What does Object.freeze() do?
  5. Can a frozen object be modified? What about its nested values?

🏁 Day 7 Wrap-Up

Tomorrow in Day 8, we’ll learn about Loops with Objects & Arrays β€” how to iterate over collections efficiently.

β˜• If you liked this content, you can support me by buying a coffee:
Buy Me A Coffee

Top comments (0)