Track: JavaScript Basics
Lesson: JS-07 / JS-10
π― Lesson Goal
By the end of this lesson, you will understand how JavaScript objects work, how to create and access them, and how to use objects to represent real-world data in applications.
π Prerequisites
- JS-01: Variables and Data Types
- JS-02: Operators and Expressions
- JS-03: Conditional Statements
- JS-04: Loops
- JS-05: Functions
- JS-06: Arrays
You should already understand:
- Variables, arrays, and functions
π What is an Object in JavaScript?
An object is a collection of keyβvalue pairs.
Each key is called a property, and the value can be:
- Number
- String
- Boolean
- Array
- Function
- Another object
Real-world analogy
A student has:
- name
- age
- course
- marks
All these belong together β perfect use case for an object.
πΉ Creating an Object (Object Literal)
let student = {
name: "Riya",
age: 21,
course: "BCA",
isActive: true
};
πΉ Accessing Object Properties
Dot notation (recommended)
console.log(student.name);
console.log(student.course);
Bracket notation
console.log(student["age"]);
β Bracket notation is useful when keys are dynamic.
πΉ Modifying Object Properties
student.age = 22;
student.city = "Delhi";
console.log(student);
πΉ Objects with Functions (Methods)
When a function is inside an object, it is called a method.
let user = {
name: "Aman",
login: function () {
console.log("User logged in");
}
};
user.login();
πΉ Using this Keyword
The this keyword refers to the current object.
let employee = {
name: "Neha",
salary: 50000,
showDetails: function () {
console.log(this.name + " earns " + this.salary);
}
};
employee.showDetails();
πΉ Objects Inside Arrays (Very Common!)
let students = [
{ name: "Aman", marks: 85 },
{ name: "Riya", marks: 92 },
{ name: "Karan", marks: 76 }
];
Looping through objects
for (let i = 0; i < students.length; i++) {
console.log(students[i].name, students[i].marks);
}
πΉ Nested Objects
Objects can contain other objects.
let order = {
orderId: 101,
customer: {
name: "Rahul",
city: "Mumbai"
},
amount: 2500
};
console.log(order.customer.name);
πΉ Checking if Property Exists
console.log("name" in student); // true
console.log("grade" in student); // false
πΉ Object vs Array (Important Difference)
| Feature | Object | Array |
|---|---|---|
| Structure | Keyβvalue | Indexed |
| Use case | Real-world entities | Lists |
| Access | By key | By index |
πΉ Real-World Example (API-style Object)
let product = {
id: 1,
title: "Laptop",
price: 55000,
tags: ["electronics", "computer"],
available: true
};
This structure looks like JSON, which is heavily used in APIs and MERN apps.
β οΈ Common Mistakes
- Forgetting commas between properties
- Using
=instead of: - Confusing array and object syntax
- Forgetting
thisinside methods
π§ Practice Tasks
- Create an object for a book
- Add a method to display book details
- Modify a property value
- Create an array of student objects
- Loop and print names of all students
π§ͺ Mini Coding Challenge
let employee = {
name: "Suresh",
department: "IT",
salary: 40000
};
Tasks:
- Add a method to increase salary by 10%
- Display updated salary
- Add a new property
location
π Summary
- Objects store related data together
- Properties use keyβvalue format
- Methods are functions inside objects
- Objects model real-world entities
- Objects are the foundation of APIs & MERN
β‘οΈ Next Lesson
JavaScript Array Methods (JS-08)
(map, filter, reduce β very important!)
π Quiz & Badge
Quiz and badges will be enabled soon.