📅 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
};
🔍 Accessing Object Properties
You can access object properties in two ways:
1. Dot Notation
console.log(user.name); // Smriti
2. Bracket Notation
console.log(user["age"]); // 24
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
You can also delete properties:
delete user.isMember;
🧭 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"
❄️ 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"
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);
❓ Interview Questions (Day 7 Topics)
- What is the difference between dot notation and bracket notation?
- How do you add or update properties in an object?
- How can you represent nested data in JavaScript?
- What does Object.freeze() do?
- 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: