# Understanding Objects in JavaScript

Okay, objects are among the most important concepts in JavaScript. Almost everything in JavaScript revolves objects.

## What objects are and why they are needed :

An object is a collection of related data stored in the form of key-value pairs.

A student has:

*   name
    
*   age
    
*   class
    
*   roll number
    

Instead of storing all these separately, we can store them inside one object.

![](https://cdn.hashnode.com/uploads/covers/696b101f5920b7f192ba7dea/64a6dc46-918a-4165-bf64-70904e797f41.png align="center")

Objects are needed, so you might think, "What's the need for creating objects?" Without objects we can:

*   organize related data
    
*   write cleaner code, as seen in the above code
    
*   manage complex applications
    
*   represent real-world entities, such as in e-commerce websites, social media apps, etc.
    

## Creating objects :

Objects are created by using curly braces { }.

**Example : User Object**  

![](https://cdn.hashnode.com/uploads/covers/696b101f5920b7f192ba7dea/69125820-ed55-4420-af88-2218dba96256.png align="center")

Here, name is the key and Vishal is the value. The object can store strings, numbers, booleans, arrays, functions, and even other objects. An object can contain another object.

## **Accessing properties inside the object :**

**1- Dot Notation :**

![](https://cdn.hashnode.com/uploads/covers/696b101f5920b7f192ba7dea/d5c58f43-fcb0-484d-a882-bfe7f628d2dd.png align="center")

**2- Bracket Notation :** When property names are dynamic or contain spaces, we use bracket notation.  

![](https://cdn.hashnode.com/uploads/covers/696b101f5920b7f192ba7dea/e9ea66cf-6572-456c-af38-bca860252c93.png align="center")

The value we want to access will comes inside string format only.

## Updating object properties :

We can also change the existing value inside objects.

For example:

![](https://cdn.hashnode.com/uploads/covers/696b101f5920b7f192ba7dea/6f75f80b-dcaa-4675-82f5-65ba86c2ed78.png align="center")

This is important because real-world applications constantly need to update data, such as user profiles, user scores, and more.

## Adding and deleting properties :

Objects are dynamic, which means we can add new properties or remove existing properties.  
**Example : Adding Properties**

![](https://cdn.hashnode.com/uploads/covers/696b101f5920b7f192ba7dea/d9954409-dcbb-45dc-b3dd-5ca3d8684274.png align="center")

**Deleting Properties :**

![](https://cdn.hashnode.com/uploads/covers/696b101f5920b7f192ba7dea/5cfab897-babd-4fb2-8adf-8354caa4ed38.png align="center")

## Looping through object keys :

Sometimes objects contain many properties. Instead of accessing them one by one, we can loop through the properties. JavaScript provides a for...in loop for objects.

Example :

![](https://cdn.hashnode.com/uploads/covers/696b101f5920b7f192ba7dea/222a3e85-f2d6-44fb-9b74-fc758b4ee705.png align="center")

  
**For accessing values :**  

![](https://cdn.hashnode.com/uploads/covers/696b101f5920b7f192ba7dea/dfd6b3a1-fd57-472b-bf01-4bf48197ff8b.png align="center")

##   
Conclusion :

Objects are one of the most powerful features of JavaScript because they help us store and manage related data efficiently.
