DEV Community

Athithya Sivasankarar
Athithya Sivasankarar

Posted on

Understanding JavaScript Objects Using CRUD Operations

Objects are one of the most important concepts in JavaScript.
They allow us to store data in key-value pairs.

What is an Object?

An object is a collection of properties where each property has a key and a value.

Example:

const user = {
    name: "Athithya",
    age: 20
};

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

Output:

{ name: "Athithya", age: 20 }

CRUD Operations in JavaScript Objects

CRUD stands for:

  • Create
  • Read
  • Update
  • Delete

1. Create (Adding Data)

We can create an object or add new properties.

Example:

const user = {};

user.name = "Athithya";
user.age = 20;

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

Output:

{ name: "Athithya", age: 20 }

2. Read (Access Data)

We can access object values using dot notation.

Example:

const user = {
    name: "Athithya",
    age: 20
};

console.log(user.name);
Enter fullscreen mode Exit fullscreen mode

Output:

Athithya

3. Update (Modify Data)

We can change existing values.

Example:

const user = {
    name: "Athithya",
    age: 20
};

user.age = 21;

console.log(user.age);
Enter fullscreen mode Exit fullscreen mode

Output:

21

4. Delete (Remove Data)

We can remove properties using the delete keyword.

Example:

const user = {
    name: "Athithya",
    age: 20
};

delete user.age;

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

Output:

{ name: "Athithya" }

Reference

Top comments (0)