While learning JavaScript, I recently started understanding functions.
A function is a reusable block of code that performs a specific task.
Basic Function
function greet() {
console.log("Hello John!");
}
greet();
Output:
Hello John!
We can also pass values using parameters:
function greet(name) {
console.log("Hello " + name);
}
greet("John");
Here, name is the parameter, and "John" is the argument.
Returning a Value
Functions can return a value using return:
function add(a, b) {
return a + b;
}
let result = add(10, 20);
console.log(result);
Output:
30
Arrow Function
JavaScript also provides a shorter syntax:
const add = (a, b) => a + b;
The main thing I learned:
Functions help us write reusable, organized, and less repetitive code.
I'm continuing my JavaScript fundamentals step by step.
Top comments (0)