# Understanding Objects in JavaScript

When you start learning JavaScript, one of the most important concepts you will encounter is Objects. Objects allow us to store related information together in a structured way.

In this article, we will understand what objects are, why we use them, and how to work with them in JavaScript.

* * *

## What are Objects - and why do we need them

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

Think of an object as a **real-world entity**.

For example, a person has a name, an age and a city. Now we can store them as:  
let person = "Priya";

let age = 28;

let city = "Mumbai";

Now this works but likely to be messy very quickly. What we have 30 people? We need to create 90 variables, all loosely related with no clear connection. That is exactly where objects come in.  
An object is a single container that groups related data together under one name. It stores the information in key-value pairs - where each key is a label, and the value is the data attached to that label.

Think of an object like a contact card in your phone. The card has fields(keys) like Name, Phone and City. Each field stores a specific piece of information (value). Everything about that person is grouped in one place.

* * *

## Creating Objects

The most common way to create an object is using curly braces {}. This is called an **object literal.**

```javascript
let person = {
  name: "Priya",
  age: 28,
  city: "Mumbai"
};
```

Here:

*   'person' is the name of the variable holding our object.
    
*   'name', 'age' and 'city' are the keys (also called as properties).
    
*   'Priya', 28, 'Mumbai' are the corresponding values.
    

**Objects vs Arrays — What's the Difference?**

A common beginner question: when do I use an array, and when do I use an object?

<table style="min-width: 75px;"><colgroup><col style="min-width: 25px;"><col style="min-width: 25px;"><col style="min-width: 25px;"></colgroup><tbody><tr><td colspan="1" rowspan="1"><p>&nbsp;</p></td><td colspan="1" rowspan="1"><p><strong>Array [ ]</strong></p></td><td colspan="1" rowspan="1"><p><strong>Object { }</strong></p></td></tr><tr><td colspan="1" rowspan="1"><p><strong>Stores</strong></p></td><td colspan="1" rowspan="1"><p>Ordered list of values</p></td><td colspan="1" rowspan="1"><p>Named key-value pairs</p></td></tr><tr><td colspan="1" rowspan="1"><p><strong>Access by</strong></p></td><td colspan="1" rowspan="1"><p>Index number (0, 1, 2…)</p></td><td colspan="1" rowspan="1"><p>Key name (name, age…)</p></td></tr><tr><td colspan="1" rowspan="1"><p><strong>Best for</strong></p></td><td colspan="1" rowspan="1"><p>Lists of similar items</p></td><td colspan="1" rowspan="1"><p>Describing one entity</p></td></tr><tr><td colspan="1" rowspan="1"><p><strong>Example</strong></p></td><td colspan="1" rowspan="1"><p>["Priya", "Arjun", "Kiran"]</p></td><td colspan="1" rowspan="1"><p>{ name: "Priya", age: 28 }</p></td></tr></tbody></table>

* * *

## **Accessing Properties**

Once you have an object, you need to read its values. JavaScript gives you two ways to do this.

**Dot Notation**

The simplest and most common approach:

```javascript
console.log(person.name);  // "Priya"
console.log(person.age);   // 28
console.log(person.city);  // "Mumbai"
```

**Bracket Notation**

You can also use square brackets with the key as a string:

```javascript
console.log(person["name"]);  // "Priya"
console.log(person["age"]);   // 28
```

Bracket notation is especially useful when the key is stored in a variable:

```javascript
let key = "city";
console.log(person[key]);  // "Mumbai"
```

**Which one should you use?**

Use dot notation whenever possible — it is shorter and easier to read. Use bracket notation when the key name has spaces, special characters, or when the key is stored dynamically in a variable.

* * *

## **Updating Object Properties**

Objects are mutable — you can change their values at any time using either dot or bracket notation.

```javascript
let person = {
  name: "Priya",
  age: 28,
  city: "Mumbai"
};

// Update using dot notation
person.age = 29;

// Update using bracket notation
person["city"] = "Delhi";

console.log(person);
// { name: "Priya", age: 29, city: "Delhi" }
```

* * *

## **Adding and Deleting Properties**

**Adding a New Property:** You can add a new key to an existing object simply by assigning it:

```javascript
person.email = "priya@example.com";

console.log(person.email);  // "priya@example.com"
```

**Deleting a Property:** Use the delete keyword to remove a property from an object:

```javascript
delete person.email;

console.log(person.email);  // undefined
```

*Note:  
After deletion, accessing the removed key returns undefined.*

*The delete keyword only works on object properties — not variables*

* * *

## **Looping Through Object Keys**

**To iterate over all the properties in an object, use a for...in loop:**

```javascript
let person = {
  name: "Priya",
  age: 28,
  city: "Mumbai"
};

for (let key in person) {
  console.log(key + ": " + person[key]);
}

// Output:
// name: Priya
// age: 28
// city: Mumbai
```

In each iteration, key holds the property name (a string), and person\[key\] retrieves the corresponding value. Notice that bracket notation is required here since key is a variable.

![](https://cdn.hashnode.com/uploads/covers/696bc200bc16f680ba8935ac/fbda1136-fc2b-4a63-8a2c-b997978a7225.png align="center")

* * *

**Practice Assignment**

Task: Build a Student Object

```plaintext
Instructions
1.  Create an object called student with at least three properties: name, age, and course.
2.  Update the age property to a new value.
3.  Add a new property called grade.
4.  Use a for...in loop to print all keys and values.
```

* * *

# Quick Summary

<table style="min-width: 50px;"><colgroup><col style="min-width: 25px;"><col style="min-width: 25px;"></colgroup><tbody><tr><td colspan="1" rowspan="1"><p><strong>Concept</strong></p></td><td colspan="1" rowspan="1"><p><strong>What It Does</strong></p></td></tr><tr><td colspan="1" rowspan="1"><p><strong>Object literal { }</strong></p></td><td colspan="1" rowspan="1"><p>Creates an object with key-value pairs</p></td></tr><tr><td colspan="1" rowspan="1"><p><strong>Dot notation</strong></p></td><td colspan="1" rowspan="1"><p>Access/update a property: <a target="_self" rel="noopener noreferrer nofollow" class="text-primary underline underline-offset-2 hover:text-primary/80 cursor-pointer" href="http://person.name" style="pointer-events: none;">person.name</a></p></td></tr><tr><td colspan="1" rowspan="1"><p><strong>Bracket notation</strong></p></td><td colspan="1" rowspan="1"><p>Access with variable key: person[key]</p></td></tr><tr><td colspan="1" rowspan="1"><p><strong>Adding property</strong></p></td><td colspan="1" rowspan="1"><p>Assign to new key: obj.newKey = value</p></td></tr><tr><td colspan="1" rowspan="1"><p><strong>delete keyword</strong></p></td><td colspan="1" rowspan="1"><p>Removes a property from the object</p></td></tr><tr><td colspan="1" rowspan="1"><p><strong>for...in loop</strong></p></td><td colspan="1" rowspan="1"><p>Iterates over all keys in an object</p></td></tr></tbody></table>
