Objects

Objects are one of the fundamental data structures in JavaScript. They allow you to store collections of related data and more complex entities. Understanding how to create, manipulate, and use objects is essential for effective JavaScript programming.

1. What is an Object?

An object is a collection of key-value pairs, where each key is a string (also called a property) and the value can be any data type, including other objects, arrays, and functions.

Example:

let person = {
  name: "John",
  age: 30,
  isStudent: true
};

console.log(person.name); // "John"
console.log(person.age);  // 30
console.log(person.isStudent); // true

In this example, person is an object with three properties: name, age, and isStudent.

2. Accessing Object Properties

You can access object properties using dot notation or bracket notation.

  • Dot Notation:

    console.log(person.name); // "John"
  • Bracket Notation:

    console.log(person["name"]); // "John"

Bracket notation is useful when the property name is stored in a variable or contains special characters.

Example:

let propertyName = "age";
console.log(person[propertyName]); // 30

3. Adding and Modifying Properties

You can add new properties or modify existing ones using dot notation or bracket notation.

Example:

// Adding a new property
person.gender = "male";
console.log(person.gender); // "male"

// Modifying an existing property
person.age = 31;
console.log(person.age); // 31

4. Removing Properties

You can remove properties from an object using the delete operator.

Example:

delete person.isStudent;
console.log(person.isStudent); // undefined

5. Methods

Objects can also contain functions, which are called methods when they are properties of an object.

Example:

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

person.greet(); // "Hello, Alice"

In this example, greet is a method of the person object that prints a greeting message using the name property.

6. Iterating Over Object Properties

You can use a for...in loop to iterate over the properties of an object.

Example:

for (let key in person) {
  console.log(key + ": " + person[key]);
}
// Output:
// name: Alice
// greet: function() { console.log("Hello, " + this.name); }

7. Nested Objects

Objects can contain other objects, allowing you to model more complex data structures.

Example:

let company = {
  name: "TechCorp",
  address: {
    street: "123 Main St",
    city: "Tech City",
    zipCode: "12345"
  }
};

console.log(company.address.city); // "Tech City"

In this example, the address property of the company object is itself an object with street, city, and zipCode properties.

8. Object Destructuring

Destructuring is a convenient way to extract values from objects and assign them to variables.

Example:

let { name, age } = person;
console.log(name); // "Alice"
console.log(age);  // 31

In this example, the name and age properties of the person object are extracted and assigned to variables.

Use Case - a Library object:

Suppose you're building an application that manages a list of books. You might represent each book as an object with properties for the title, author, and year of publication.

Example:

let book1 = {
  title: "To Kill a Mockingbird",
  author: "Harper Lee",
  year: 1960
};

let book2 = {
  title: "1984",
  author: "George Orwell",
  year: 1949
};

let library = [book1, book2];

// Adding a new book to the library
let book3 = {
  title: "The Great Gatsby",
  author: "F. Scott Fitzgerald",
  year: 1925
};
library.push(book3);

console.log(library);

In this example:

  • Each book is represented as an object with title, author, and year properties.

  • The library array contains a list of book objects.

  • A new book object is created and added to the library array.

Understanding objects is essential for managing complex data and building robust applications in JavaScript. As you continue to learn and practice, you'll see how objects are used throughout JavaScript programming and in real-world applications.


Help us improve the content 🤩

You can leave comments here.

Last updated