📝 What You’ll Learn

  • What an object is in JavaScript
  • How to define and access objects
  • How to work with object properties and methods
  • Practical examples of using objects in JavaScript

🔍 What Is an Object?

An object is a collection of key-value pairs where each key is a unique identifier (also called a property or attribute) and each value can be any data type, including other objects. Objects are one of the most important data structures in JavaScript and are widely used to represent real-world entities.

Basic Syntax

let person = {
  name: "Alice",
  age: 30,
  isActive: true
};

Here, person is an object with three properties:

  • name: a string
  • age: a number
  • isActive: a boolean

🛠️ Accessing Object Properties

You can access an object’s properties using either dot notation or bracket notation.

Dot Notation:

console.log(person.name);  // Output: Alice
console.log(person.age);   // Output: 30

Bracket Notation:

Bracket notation is useful when property names are dynamic or not valid JavaScript identifiers (like containing spaces).

console.log(person["name"]);  // Output: Alice
console.log(person["age"]);   // Output: 30

🔄 Modifying Object Properties

You can change an object’s property value by simply assigning a new value to it.

Example:

person.age = 31;  // Modify the age
person["isActive"] = false;  // Modify the isActive property

console.log(person.age);  // Output: 31
console.log(person.isActive);  // Output: false

🧑‍💻 Adding New Properties to Objects

You can also add new properties to an object after it’s been defined.

person.city = "New York";  // Add a new property

console.log(person.city);  // Output: New York

🔨 Methods in Objects

Objects can also have methods, which are functions stored as object properties.

Example:

let person = {
  name: "Alice",
  greet: function() {
    console.log("Hello, " + this.name);
  }
};

person.greet();  // Output: Hello, Alice

Here, the greet property is a method that prints a greeting using the name property. We used this to refer to the current object.

🏷️ Object Destructuring

Destructuring allows you to extract multiple properties from an object and assign them to variables in a more concise way.

Example:

let person = {
  name: "Alice",
  age: 30,
  city: "New York"
};

let { name, age, city } = person;

console.log(name);  // Output: Alice
console.log(age);   // Output: 30
console.log(city);  // Output: New York

🛠️ Nested Objects

Objects can contain other objects, allowing for more complex data structures.

Example:

let person = {
  name: "Alice",
  address: {
    street: "123 Main St",
    city: "New York"
  }
};

console.log(person.address.city);  // Output: New York

Here, address is an object inside the person object. You can access nested properties using dot notation.

🧠 Recap

  • Objects store key-value pairs and are used to represent real-world entities.
  • You can access, modify, and add properties using dot or bracket notation.
  • Methods in objects are functions that are part of the object.
  • Object destructuring allows for a more concise way to access properties.
  • Objects can be nested, meaning they can contain other objects.