# Understanding Objects in JavaScript

When programs become larger, we often need to store related information together.

For example, imagine storing information about a person:

*   name
    
*   age
    
*   city
    

If we store each value in separate variables, the code quickly becomes messy. JavaScript solves this problem using **objects**.

An **object** is a collection of related data stored as **key–value pairs**.

Think of it like a real-world record. Each piece of information has a **name (key)** and a **value**.

### Creating an Object

Creating an object in JavaScript is simple. We use **curly braces** `{}` and define properties inside them.

Example:

```plaintext
let person = {
  name: "Alex",
  age: 25,
  city: "London"
};

console.log(person);
```

Here:

*   `name`, `age`, `city` → keys
    
*   `"Alex"`, `25`, `"London"` → values
    

So the object stores **related information in one place**.

**Object Structure**

![](https://cdn.hashnode.com/uploads/covers/69785a22373bd4608f33fced/263d8189-c754-4fca-b2fc-65ebbd889f36.png align="center")

### Accessing Object Properties

To read values from an object, we can use two methods.

### Dot Notation

The most common way is using a dot.

```plaintext
console.log(person.name);
```

Output:

```plaintext
Alex
```

### Bracket Notation

Another way is using brackets.

```plaintext
console.log(person["age"]);
```

Output:

```plaintext
25
```

Bracket notation is useful when the property name is stored in a variable.

### Updating Object Properties

Objects are flexible. We can change values whenever needed.

Example:

```plaintext
person.age = 26;

console.log(person.age);
```

Now the age is updated from **25 to 26**.

### Adding New Properties

Objects can also grow over time.

We can add new properties simply by assigning them.

Example:

```plaintext
person.country = "UK";

console.log(person);
```

Now the object includes a new key **country**.

### Deleting Properties

Sometimes we need to remove data from an object.

JavaScript provides the **delete** keyword.

Example:

```plaintext
delete person.city;

console.log(person);
```

Now the `city` property is removed.

**Array vs Object**

![](https://cdn.hashnode.com/uploads/covers/69785a22373bd4608f33fced/6cace94d-ef36-45a4-af25-57cffb197bc4.png align="center")

### Looping Through Object Properties

When objects become larger, we may want to read all keys and values.

JavaScript allows this using a **for...in loop**.

Example:

```plaintext
for (let key in person) {
  console.log(key, person[key]);
}
```

Output might look like:

```plaintext
name Alex
age 26
country UK
```

This loop goes through every property in the object.

### Conclusion

Objects are one of the most important structures in JavaScript. They allow us to store related information together using **key–value pairs**.

Instead of managing many separate variables, objects help organize data in a clear and structured way. As programs grow larger, objects become essential for managing complex information.
