DEV Community

Cover image for JavaScript Functions & CRUD Operations on Objects 🚀
Narasimma Radhakrishnan
Narasimma Radhakrishnan

Posted on

JavaScript Functions & CRUD Operations on Objects 🚀

As a beginner in JavaScript, I learned that functions help us organize and reuse code, while objects help us store related data.

Let's understand both with simple examples.

  1. Types of Functions in JavaScript Normal Function

A normal function is created using the function keyword.

function greet() {
  console.log("Hello!");
}
greet();
Enter fullscreen mode Exit fullscreen mode

Function with Parameters

We can pass values to a function.

function greet(name) {
  console.log("Hello " + name);
}
greet("Narasimma");
Enter fullscreen mode Exit fullscreen mode

Function with Return

A function can return a value.

function add(a, b) {
  return a + b;
}

console.log(add(10, 20));
Enter fullscreen mode Exit fullscreen mode

Output:

30
Arrow Function

Arrow functions provide a shorter way to write functions.

const add = (a, b) => {
  return a + b;
};

console.log(add(10, 20));
Enter fullscreen mode Exit fullscreen mode

CRUD Operations on Objects 📦

CRUD stands for:

C → Create
R → Read
U → Update
D → Delete

Let's use a simple student object.

Create

Create an object with some information.

let student = {
  name: "Narasimma",
  age: 21,
  course: "B.Tech IT"
};
Enter fullscreen mode Exit fullscreen mode

Read

Access the information from the object.

console.log(student.name);
console.log(student.age);

Output:

Narasimma
21
Update

Change an existing value.

student.age = 22;

console.log(student.age);

Output:

22

We can also add a new property:

student.city = "Chennai";

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

Delete

Remove a property using the delete keyword.

delete student.age;

console.log(student);

Now the age property is removed.

Easy Way to Remember 💡

Functions = Perform actions ⚙️

Objects = Store information 📦

CRUD = Create → Read → Update → Delete

These are simple concepts, but they are very important when working with JavaScript applications and APIs.

Keep practicing small programs before moving to bigger projects! 🚀

javascript #webdev #beginners #coding

Top comments (0)