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.
- Types of Functions in JavaScript Normal Function
A normal function is created using the function keyword.
function greet() {
console.log("Hello!");
}
greet();
Function with Parameters
We can pass values to a function.
function greet(name) {
console.log("Hello " + name);
}
greet("Narasimma");
Function with Return
A function can return a value.
function add(a, b) {
return a + b;
}
console.log(add(10, 20));
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));
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"
};
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);
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! 🚀
Top comments (0)