JavaScript Objects (JS-07)

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)

FeatureObjectArray
StructureKeyโ€“valueIndexed
Use caseReal-world entitiesLists
AccessBy keyBy 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 this inside methods

๐Ÿง  Practice Tasks

  1. Create an object for a book
  2. Add a method to display book details
  3. Modify a property value
  4. Create an array of student objects
  5. 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.

Leave a Reply

Your email address will not be published. Required fields are marked *