JavaScript Functions – A Beginner-Friendly Introduction
Today I learned about Functions in JavaScript.
A function is a reusable block of code that performs a specific task. Instead of writing the same code again and again, we can create a function and call it whenever we need it.
Creating a Function
function greet() {
console.log("Hello, World!");
}
Here, "greet" is the function name.
Calling a Function
To execute the function, we need to call it:
greet();
Output:
Hello, World!
🔹 Function with Parameters
Functions can accept values using parameters.
function greet(name) {
console.log("Hello " + name);
}
greet("Jeevish");
Output:
Hello Jeevish
Return Statement
A function can return a value using the "return" statement.
function add(a, b) {
return a + b;
}
let result = add(10, 20);
console.log(result);
- What is a Function?
- Function Declaration
- Function Calling
- Parameters and Arguments
- Return Statement
- Reusing code with Functions
Functions are an important part of JavaScript because they help us write clean, reusable, and organized code.
I'm continuing my JavaScript learning journey step by step
Output:
30
📚 What I Learned Today
Top comments (0)