DEV Community

Cover image for Objects in JavaScript
Harman Panwar
Harman Panwar

Posted on

Objects in JavaScript

In many programs, we need to represent real-world entities such as a person, a student, a product, or a car. These entities often have multiple related properties.

JavaScript provides objects to organize this type of data in a clear and structured way.


What Are Objects and Why Do We Need Them?

An object is a data structure that stores information as key-value pairs.

Each piece of data has:

  • a key (also called a property name)
  • a value (the data stored in that property)

Example:

let person = {
  name: "Alice",
  age: 25,
  city: "London"
};
Enter fullscreen mode Exit fullscreen mode

Here:

Key Value
name "Alice"
age 25
city "London"

Objects help us:

  • Represent real-world data
  • Organize related information together
  • Access and update data easily

Array vs Object

It is helpful to understand the difference between arrays and objects.

Array

Arrays store ordered values and are accessed using indexes.

let fruits = ["Apple", "Banana", "Mango"];

console.log(fruits[0]); // Apple
Enter fullscreen mode Exit fullscreen mode

Object

Objects store named values and are accessed using keys.

let person = {
  name: "Alice",
  age: 25
};

console.log(person.name); // Alice
Enter fullscreen mode Exit fullscreen mode

Summary:

Feature Array Object
Structure Ordered list Key-value pairs
Access Index (0,1,2) Key name
Use case Lists of values Structured data

Creating Objects

Objects are created using curly braces {}.

Example:

let person = {
  name: "John",
  age: 30,
  city: "New York"
};
Enter fullscreen mode Exit fullscreen mode

Each property is written as:

key: value
Enter fullscreen mode Exit fullscreen mode

Multiple properties are separated by commas.


Accessing Properties

There are two ways to access object properties:

  • Dot notation
  • Bracket notation

Dot Notation

This is the most common way.

let person = {
  name: "John",
  age: 30
};

console.log(person.name);
console.log(person.age);
Enter fullscreen mode Exit fullscreen mode

Output:

John
30
Enter fullscreen mode Exit fullscreen mode

Bracket Notation

Properties can also be accessed using brackets.

let person = {
  name: "John",
  age: 30
};

console.log(person["name"]);
console.log(person["age"]);
Enter fullscreen mode Exit fullscreen mode

Output:

John
30
Enter fullscreen mode Exit fullscreen mode

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

Example:

let key = "name";
console.log(person[key]);
Enter fullscreen mode Exit fullscreen mode

Updating Object Properties

You can update the value of an existing property.

Example:

let person = {
  name: "John",
  age: 30
};

person.age = 31;

console.log(person);
Enter fullscreen mode Exit fullscreen mode

Output:

{ name: "John", age: 31 }
Enter fullscreen mode Exit fullscreen mode

Adding New Properties

New properties can be added easily.

Example:

let person = {
  name: "John",
  age: 30
};

person.city = "Paris";

console.log(person);
Enter fullscreen mode Exit fullscreen mode

Output:

{ name: "John", age: 30, city: "Paris" }
Enter fullscreen mode Exit fullscreen mode

Deleting Properties

Properties can be removed using the delete keyword.

Example:

let person = {
  name: "John",
  age: 30,
  city: "Paris"
};

delete person.city;

console.log(person);
Enter fullscreen mode Exit fullscreen mode

Output:

{ name: "John", age: 30 }
Enter fullscreen mode Exit fullscreen mode

Looping Through Object Keys

To go through all properties of an object, we can use a for...in loop.

Example:

let person = {
  name: "Alice",
  age: 25,
  city: "London"
};

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

Output:

name Alice
age 25
city London
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • key represents each property name
  • person[key] retrieves its value

Assignment

Questions

  1. Create an object representing a student.

  2. Add properties such as:

  • name
  • age
  • course
  1. Update one property.

  2. Print all keys and values using a loop.


Answers

1. Create a Student Object

let student = {
  name: "Rahul",
  age: 20,
  course: "Computer Science"
};
Enter fullscreen mode Exit fullscreen mode

2. Update One Property

student.age = 21;

console.log(student);
Enter fullscreen mode Exit fullscreen mode

Output:

{ name: "Rahul", age: 21, course: "Computer Science" }
Enter fullscreen mode Exit fullscreen mode

3. Print All Keys and Values

for (let key in student) {
  console.log(key, student[key]);
}
Enter fullscreen mode Exit fullscreen mode

Output:

name Rahul
age 21
course Computer Science
Enter fullscreen mode Exit fullscreen mode

Conclusion

Objects are an essential part of JavaScript because they allow us to represent structured data using key-value pairs. They are commonly used to represent real-world entities such as users, products, or students.

By learning how to create objects, access properties, update values, and loop through keys, you build a strong foundation for working with more complex data in JavaScript applications.

Top comments (0)