Function Parameters and Return Values0%

Function Parameters and Return Values

Beginner12 min read•Updated: Jul 11, 2026
Study Materials

Function Parameters and Return Values

Functions receive inputs via parameters and send outputs back to the caller via return values. Mastering parameter defaults, rest parameters, and return semantics is essential for writing robust JavaScript functions.


1. Parameters vs. Arguments

While often used interchangeably in casual conversation, there is a clear distinction:

  • Parameters: The variable names listed in the function definition.
  • Arguments: The real values passed into the function when you invoke it.
JavaScript
// 'base' and 'exponent' are PARAMETERS:
function power(base, exponent) {
return base ** exponent;
}
 
// 2 and 3 are ARGUMENTS:
const result = power(2, 3);

2. The return Statement

The return statement does two things:

  1. 1
    It immediately stops the execution of the function.
  2. 2
    It sends the evaluated value back to the caller.
JavaScript
function calculateTax(subtotal, taxRate) {
if (subtotal <= 0) {
return 0; // Exits early!
}
 
return subtotal * taxRate;
}
 
const tax = calculateTax(1000, 0.18); // 180
[!IMPORTANT] If a function does not have an explicit return statement (or has an empty return;), it automatically returns undefined!
JavaScript
function doNothing() {}
console.log(doNothing()); // undefined

3. Default Parameters (ES6)

What happens if a caller forgets to pass an argument? Without defaults, missing arguments become undefined. In ES6, you can specify default values directly in the parameter list:

JavaScript
function createAccount(username, role = "Student", isActive = true) {
return {
username,
role,
isActive
};
}
 
console.log(createAccount("sumit_dev"));
// { username: "sumit_dev", role: "Student", isActive: true }

4. Variable Number of Arguments: Rest Parameters (...args)

What if you want to write a function that can accept any number of inputs (e.g. summing 3, 5, or 100 numbers)? Use the Rest Parameter (...):

JavaScript
function sumAll(...numbers) {
// 'numbers' is a real JavaScript array!
let total = 0;
for (const n of numbers) {
total += n;
}
return total;
}
 
console.log(sumAll(10, 20)); // 30
console.log(sumAll(5, 15, 25, 35, 45)); // 125
Note
[!NOTE] The rest parameter must always be the very last parameter in the function definition!

5. Returning Multiple Values via Objects or Arrays

A function in JavaScript can technically only return a single value. To return multiple values, return an object or an array:

JavaScript
function getMinMax(numbers) {
const min = Math.min(...numbers);
const max = Math.max(...numbers);
return { min, max };
}
 
// Destructure the returned object:
const { min, max } = getMinMax([14, 5, 82, 1, 99]);
console.log(`Lowest: ${min}, Highest: ${max}`); // Lowest: 1, Highest: 99

Interactive Quiz

Practice Quiz

Question 1
What does a JavaScript function return by default if it lacks an explicit return statement?
Question 2
What is the difference between a parameter and an argument?
Question 3
What is the value of tax if invoked as calculateTotal(500) where the function is defined as function calculateTotal(price, tax = 0.1) { return price * tax; }?
Question 4
Which operator allows a function to collect an indefinite number of arguments into a single array?
Question 5
Can code written after a return statement in the same execution branch ever execute?
Next Lesson

Arrow Functions (Basic)

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

PrevNext