Skip to main content

Command Palette

Search for a command to run...

Understanding Objects in JavaScript

Updated
3 min readView as Markdown

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:

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

Accessing Object Properties

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

Dot Notation

The most common way is using a dot.

console.log(person.name);

Output:

Alex

Bracket Notation

Another way is using brackets.

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

Output:

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:

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:

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:

delete person.city;

console.log(person);

Now the city property is removed.

Array vs Object

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:

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

Output might look like:

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.