In JavaScript, parameters and return values define how functions receive input and produce output. Understanding them is crucial for writing reusable and predictable code.
Parameters are variables listed in a function’s definition. They act as placeholders for the values (arguments) that will be passed when the function is called.
function add(a, b) {
return a + b;
}Here:
aandbare parameters.- They represent values supplied during function execution.
Must be provided when calling the function.
function greet(name) {
console.log("Hello, " + name);
}
greet("Brandon"); // Output: Hello, BrandonUsed when no argument is provided.
function greet(name = "Guest") {
console.log("Hello, " + name);
}
greet(); // Output: Hello, GuestCapture multiple arguments into an array.
function sum(...numbers) {
return numbers.reduce((acc, val) => acc + val, 0);
}
console.log(sum(1, 2, 3, 4)); // Output: 10Used for objects or arrays.
function displayUser({ name, age }) {
console.log(`${name} is ${age} years old`);
}
displayUser({ name: "Alice", age: 25 }); // Output: Alice is 25 years old- Parameters: variables in the function definition.
- Arguments: actual values passed to those parameters when the function is called.
function multiply(x, y) { // x, y → parameters
return x * y;
}
multiply(4, 5); // 4, 5 → argumentsThe return value is the output produced when a function finishes executing. Defined using the return keyword.
function square(num) {
return num * num;
}
let result = square(4); // result = 16- The
returnstatement ends the function execution immediately. - A function without
returnreturnsundefined.
function noReturn() {
let a = 10;
}
console.log(noReturn()); // undefinedA function can return multiple values via arrays or objects.
Using Arrays
function getCoordinates() {
return [10, 20];
}
const [x, y] = getCoordinates();
console.log(x, y); // 10 20Using Objects
function getUser() {
return { name: "Brandon", age: 30 };
}
const { name, age } = getUser();
console.log(name, age); // Brandon 30The return statement stops further execution.
function check(num) {
if (num > 0) return "Positive";
if (num < 0) return "Negative";
return "Zero";
}
console.log(check(5)); // Positive- Keep parameter lists short (prefer objects for many parameters).
- Use descriptive parameter names.
- Always return consistent types.
- Use default parameters instead of checking for
undefined. - Document both parameters and return values in code comments.
/**
* Calculates total price with tax.
* @param {number} price - Base price.
* @param {number} taxRate - Tax percentage.
* @returns {number} Total price including tax.
*/
function calculateTotal(price, taxRate = 0.1) {
return price + price * taxRate;
}| Issue | Description | Example |
|---|---|---|
| Missing return | Function doesn’t output anything | function f(x) { x + 1 } → undefined |
| Wrong parameter order | Arguments mapped incorrectly | function divide(a, b) { return a/b } → divide(2, 10) ≠ divide(10, 2) |
| Mutating parameters | Changing input objects inside a function | Avoid side effects |
function processOrder({ items, taxRate = 0.08 }) {
const subtotal = items.reduce((sum, item) => sum + item.price, 0);
const total = subtotal + subtotal * taxRate;
return { subtotal, total };
}
const order = {
items: [
{ name: "Headphones", price: 50 },
{ name: "Keyboard", price: 80 }
],
taxRate: 0.1
};
const { subtotal, total } = processOrder(order);
console.log(subtotal, total); // 130 143- Define parameters and arguments and explain the difference.
- What happens when a function does not include a
returnstatement? - Write a function with default and rest parameters to calculate average scores.
- How can multiple values be returned from one function? Show both array and object methods.
- Explain why mutating parameters inside functions is discouraged.
- Implement a function
calculateBMI(weight, height)that returns both BMI value and category. - How does destructuring in parameters simplify function usage? Provide an example.
- What is the purpose of default parameters in function definitions?
- Demonstrate control flow using
returnin conditional branches. - Comment your code using JSDoc to describe parameters and return values for a simple function.