What is an object?

👨‍💻 Frontend Developer 🟡 Often Asked 🎚️ Easy
#JavaScript #JS Basics #Objects

Short Answer

An object is a data structure in JavaScript that stores values in key–value pairs. Objects are used to describe entities with multiple characteristics (e.g., a user with a name, age, access rights, etc.).


What is an object?

An object in JavaScript is a collection of properties. Each property is a key (name) and a value.

const user = {
  name: "Anya",
  age: 28,
  isAdmin: true
};

Why do we need objects?

  • To store complex data structures
  • To group variables into a single logical unit
  • To describe real-world entities (product, order, user)
  • To pass parameters to functions or return them
  • To make code more readable and maintainable

How to create an object?

1. Object literal

const car = {
  brand: "Toyota",
  year: 2022
};

2. Using new Object()

const car = new Object();
car.brand = "Toyota";
car.year = 2022;

Accessing object properties

Dot notation

console.log(user.name); // "Anya"

Bracket notation

const key = "age";
console.log(user[key]); // 28

Modifying and deleting properties

user.age = 30;
delete user.isAdmin;

Nested objects

const company = {
  name: "TechCorp",
  address: {
    city: "Moscow",
    street: "Lenina, 5"
  }
};

Iterating over object properties

for (let key in user) {
  console.log(key, user[key]);
}

Checking if a property exists

console.log("name" in user); // true

Conclusion

Objects are a fundamental part of JavaScript.
They allow you to store and organize data in a flexible and convenient way.
Understanding them is key to working with most structures in JavaScript.