Day 7: Understanding Objects in JavaScript ๐
Dipak Ahirav
Posted on July 17, 2024
Introduction
Welcome to Day 7 of your JavaScript journey! ๐ Yesterday, we explored arrays. Today, we'll dive into objects, another fundamental data structure in JavaScript. Objects allow you to store collections of key-value pairs, making it easier to organize and manipulate complex data. Let's get started! ๐
please subscribe to my YouTube channel to support my channel and get more web development tutorials.
What is an Object? ๐
An object is a collection of properties, where each property is defined as a key-value pair. The key is a string (or symbol) that identifies the property, and the value can be any data type.
Example:
let person = {
name: "Alice",
age: 25,
isStudent: true
};
console.log(person.name); // Output: Alice ๐ฉโ๐
Creating Objects ๐ฑ
You can create objects in multiple ways:
1. Using Object Literals
let car = {
make: "Toyota",
model: "Camry",
year: 2021
};
2. Using the Object
Constructor
let car = new Object();
car.make = "Toyota";
car.model = "Camry";
car.year = 2021;
Accessing Object Properties ๐
You can access properties in an object using dot notation or bracket notation:
Dot Notation
let book = {
title: "JavaScript for Beginners",
author: "John Doe"
};
console.log(book.title); // Output: JavaScript for Beginners ๐
Bracket Notation
let book = {
title: "JavaScript for Beginners",
author: "John Doe"
};
console.log(book["author"]); // Output: John Doe โ๏ธ
Adding and Modifying Properties โ๏ธ
You can add new properties or modify existing ones using dot or bracket notation:
Example:
let person = {
name: "Alice",
age: 25
};
person.isStudent = true; // Adding a new property
person.age = 26; // Modifying an existing property
console.log(person); // Output: { name: "Alice", age: 26, isStudent: true } ๐
Deleting Properties ๐๏ธ
You can delete properties from an object using the delete
operator:
Example:
let person = {
name: "Alice",
age: 25,
isStudent: true
};
delete person.isStudent;
console.log(person); // Output: { name: "Alice", age: 25 } ๐ซ
Methods ๐
Methods are functions that are properties of an object. They allow objects to perform actions.
Example:
let calculator = {
add: function(a, b) {
return a + b;
},
subtract: function(a, b) {
return a - b;
}
};
console.log(calculator.add(5, 3)); // Output: 8 โ
console.log(calculator.subtract(5, 3)); // Output: 2 โ
this
Keyword ๐
The this
keyword refers to the current object in a method or function.
Example:
let person = {
name: "Alice",
greet: function() {
console.log("Hello, " + this.name + "!");
}
};
person.greet(); // Output: Hello, Alice! ๐
Practical Examples ๐งฉ
Example 1: Create an object representing a book
let book = {
title: "JavaScript: The Good Parts",
author: "Douglas Crockford",
pages: 176,
printSummary: function() {
console.log(this.title + " by " + this.author + " has " + this.pages + " pages.");
}
};
book.printSummary(); // Output: JavaScript: The Good Parts by Douglas Crockford has 176 pages. ๐
Example 2: Create a method to calculate the age of a car
let car = {
make: "Toyota",
model: "Camry",
year: 2015,
getAge: function(currentYear) {
return currentYear - this.year;
}
};
console.log("Car age:", car.getAge(2024)); // Output: Car age: 9 ๐
Practice Activities ๐ช
1. Practice Code:
- Create objects using object literals and the
Object
constructor. - Add, modify, and delete properties in objects.
- Write methods for objects and use the
this
keyword.
2. Mini Project:
- Create an object representing a student with properties for name, age, and grades. Add methods to calculate the average grade and print a summary.
Example:
let student = {
name: "Bob",
age: 20,
grades: [90, 85, 88, 92],
calculateAverage: function() {
let sum = this.grades.reduce((a, b) => a + b, 0);
return sum / this.grades.length;
},
printSummary: function() {
console.log(this.name + " is " + this.age + " years old and has an average grade of " + this.calculateAverage() + ".");
}
};
student.printSummary(); // Output: Bob is 20 years old and has an average grade of 88.75. ๐
Summary ๐
Today, we explored objects in JavaScript. We learned how to create objects, access and manipulate their properties, and write methods. Understanding objects is crucial for organizing and managing complex data in your code.
Series Index
Part | Title | Link |
---|---|---|
1 | Day 1: Getting Started with JavaScript | Read Part 1 |
2 | Day 2: Understanding Variables and Data Types in JavaScript | Read Part 2 |
3 | Day 3: Mastering Operators and Expressions in JavaScript | Read Part 3 |
4 | Day 4: Control Structures in JavaScript | Read Part 4 |
5 | Day 5: Understanding Functions in JavaScript | Read Part 5 |
6 | Day 6: Mastering Arrays in JavaScript ๐ | Read Part 6 |
7 | Day 7: Understanding Objects in JavaScript ๐ | Read Part 7 |
Stay tuned for Day 8, where we'll dive into more advanced topics in JavaScript! ๐
Happy coding! If you have any questions or need further clarification, feel free to leave a comment below. Let's continue learning and growing together! ๐
Follow me for more tutorials and tips on web development. Feel free to leave comments or questions below!
Follow and Subscribe:
- Website: Dipak Ahirav
- Email: dipaksahirav@gmail.com
- YouTube: devDive with Dipak
- LinkedIn: Dipak Ahirav
Posted on July 17, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
October 4, 2024