JavaScript Objects — Notes + Practice
Tasks
What is an Object?
● An object is a collection of key–value pairs.
● It stores related data and functions together.
Example:
const person = {
name: "Rahul",
age: 25,
city: "Chennai"
};
Accessing Object Properties
🔹 Dot Notation
[Link]([Link]); // Rahul
🔹 Bracket Notation
[Link](person["city"]); // Chennai
Adding / Updating / Deleting Properties
[Link] = "Male"; // add
[Link] = 26; // update
delete [Link]; // delete
Looping Through an Object
for (let key in person) {
[Link](key + ": " + person[key]);
}
Object Methods (Functions Inside Objects)
Objects can have methods, which are just functions stored as property values.
const student = {
name: "Meena",
mark: 85,
greet: function() {
[Link]("Hello, " + [Link]);
}
};
[Link](); // Output: Hello, Meena
Using this Keyword
● Refers to the current object inside a method.
Example:
const car = {
brand: "Toyota",
start() {
[Link]([Link] + " car started");
}
};
[Link](); // Toyota car started
Object Utility Methods
🔸 [Link](obj) → returns array of property names
[Link](person);
🔸 [Link](obj) → returns array of property values
[Link](person);
🔸 [Link](obj) → returns array of key-value pairs
[Link](person);
Practice Tasks
Task 1 – Create and Access
Create an object book with keys: title, author, and pages.
Print the author name using both dot and bracket notation.
Task 2 – Add and Update
Create an object user with properties name and age.
Add a new property email and update the age.
Task 3 – Loop Through
Create an object student with 3 subjects and marks.
Loop through and print each subject with its mark.
Task 4 – Method Example
Create an object calculator with methods:
● add(a,b) → returns sum
● sub(a,b) → returns difference
Call both methods and print results.
Task 5 – Using this
Create an object employee with properties name and salary.
Add a method details() that prints "Name: ___, Salary: ___" using this.
Task 6 – [Link]() / values()
Create an object mobile and print all its keys and values using built-in methods.
Task 7 – Nested Object
Create an object student with nested details:
{
name: "Kavi",
marks: { maths: 80, science: 90 }
}
Access and print the science mark.
Task 8 – Combine Objects
Use the spread operator (...) to merge:
const a = {name: "Ram"};
const b = {age: 20};
Result → {name: "Ram", age: 20}
Task 9 – Delete Property
Create an object car with properties brand, price, year.
Delete the year property and print the updated object.