Day 7: Understanding Objects in JavaScript ๐Ÿ†

dipakahirav

Dipak Ahirav

Posted on July 17, 2024

Day 7: Understanding Objects in JavaScript ๐Ÿ†

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 ๐Ÿ‘ฉโ€๐ŸŽ“
Enter fullscreen mode Exit fullscreen mode

Creating Objects ๐ŸŒฑ

You can create objects in multiple ways:

1. Using Object Literals

let car = {
  make: "Toyota",
  model: "Camry",
  year: 2021
};
Enter fullscreen mode Exit fullscreen mode

2. Using the Object Constructor

let car = new Object();
car.make = "Toyota";
car.model = "Camry";
car.year = 2021;
Enter fullscreen mode Exit fullscreen mode

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 ๐Ÿ“˜
Enter fullscreen mode Exit fullscreen mode

Bracket Notation

let book = {
  title: "JavaScript for Beginners",
  author: "John Doe"
};
console.log(book["author"]); // Output: John Doe โœ๏ธ
Enter fullscreen mode Exit fullscreen mode

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 } ๐ŸŽ“
Enter fullscreen mode Exit fullscreen mode

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 } ๐Ÿšซ
Enter fullscreen mode Exit fullscreen mode

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 โž–
Enter fullscreen mode Exit fullscreen mode

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! ๐Ÿ™‹
Enter fullscreen mode Exit fullscreen mode

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. ๐Ÿ“–
Enter fullscreen mode Exit fullscreen mode

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 ๐Ÿš—
Enter fullscreen mode Exit fullscreen mode

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. ๐ŸŽ“
Enter fullscreen mode Exit fullscreen mode

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:

๐Ÿ’– ๐Ÿ’ช ๐Ÿ™… ๐Ÿšฉ
dipakahirav
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

ยฉ TheLazy.dev

About