JavaScript Functions: Parameters and Return Values
A JavaScript function is a reusable block of code. Give it a clear name, pass information through parameters and return a result when needed.
Declare a function
function greet(name) {
return "Hello, " + name + "!";
}
const message = greet("Aadil");
Here, name is a parameter. Calling greet("Aadil") passes an argument, and the function returns a string.
Use an arrow function
const add = (first, second) => {
return first + second;
};
const total = add(3, 4);
For a short expression, an arrow function can return the value directly:
const double = number => number * 2;
Keep functions focused
A function is easier to understand when it performs one clear job. Use meaningful names and return values rather than relying on unexpected changes elsewhere in the page.