Skip to main content

Command Palette

Search for a command to run...

Understanding Objects in JavaScript

Updated
2 min readView as Markdown
V
I am a Btech student Experienced in building applications that solve real world problems , likes to share my knowledge on topics of web development through my blogs and the knowledge gained through the project building .

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.

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

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 :

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

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:

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

Deleting Properties :

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 :

For accessing values :

Conclusion :

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

1 views