Parameter Functions
Last Updated: 21st October 2025
A parameterized function is a function that takes input values as parameters and performs an action based on those values.It can be following types: Arguments are actual values passed when the function is called.
Parameterized function
Takes one or more parameters.
📝 Syntax:
function functionName(parameter1, parameter2, ...) {
// code block
//..
}
functionName(value1, value2, ...);
Example 1:
function add(a, b) {
return a + b;
}
console.log(add(2, 3)); // 5
Example 2:
const greet = function (name) {
console.log("Hello, " + name + "!");
};
greet("Sadhu"); // Output: "Hello, Sadhu!"
Example 3:
const sum = (a, b) => {
console.log(a + b);
};
sum(2, 3); // Output: 5
// One-line return (implicit return)
const add = (a, b) => a + b;
// Single parameter (no need for parentheses)
const square = (n) => n * n;
Default parameters
A default parameter is a parameter that has a default value if no value is provided when the function is called.
📝 Syntax:
function functionName(parameter1, parameter2 = defaultValue) {
// code block
//..
}
Example:
function greet(name = "Guest") {
console.log("Hello, " + name + "!");
}
greet(); // Output: "Hello, Guest!"
greet("Sadhu"); // Output: "Hello, Sadhu!"
Rest parameters
A rest parameter is a parameter that collects all the remaining arguments into an array.Used when number of arguments is unknown (... rest operator).
📝 Syntax:
function functionName(parameter1, parameter2, ..., parameterN) {
// code block
//..
}
Example:
function sum(...numbers) {
let total = 0;
for (const num of numbers) {
total += num;
}
console.log(total);
}
sum(1, 2, 3, 4, 5); // Output: 15
💡 Quick Practice
- Create a function add that takes two numbers and prints their sum.
- Store a function in a variable add that adds two numbers.
- Write an arrow function sub to subtract two numbers.
- Try using this inside an arrow function and see the behavior.
- Exercise Set 1
- Exercise Set 2