ReviseAlgo Logo

Functions

Parameters — Default, Rest, Destructured

Master JavaScript function parameters. Learn ES6 default parameters, rest parameters, destructured parameter objects, and argument validation patterns.

Last Updated: July 15, 2026 10 min read

1. Introduction

Parameters define the inputs a function accepts to perform its operation. ES6 modernized parameter handling by introducing Default Parameters, Rest Parameters, and Destructured Parameters, making functions more flexible and easier to read.

2. Why It Matters

Using modern parameter patterns simplifies function signatures. It removes the need for boilerplate checks inside function bodies and makes passing complex arguments cleaner.

3. Real-World Analogy

Think of a Custom Pizza Order:

  • Default Parameters: If you don't choose a crust type, the pizzeria defaults to regular crust. You only pay or change crust when you explicitly request a different style.
  • Rest Parameters: Ordering a pizza and adding "everything else you have in the tray" as toppings. The chef gathers all additional ingredients into a single container and puts them on the pizza.
  • Destructured Parameters: Instead of receiving a single massive box and opening nested packaging, the chef hands you a pre-organized box with labeled compartments for dipping sauce, garlic bread, and napkins, letting you grab exactly what you need immediately.

4. How It Works

Let's explore the three modern patterns:

1. Default Parameters:

Enables setting default values for parameters if no value or undefined is passed to the function.

2. Rest Parameters:

Uses the spread/rest operator (...) prefix to capture an arbitrary number of trailing arguments into a single array.

3. Destructured Parameters:

Unpacks property parameters from an object directly within the function signature, allowing named parameters.

5. Architectural Details

Unlike the legacy arguments object, which is array-like but lacks helper methods (like map or reduce), rest parameters create a true array. This means you can run array operations on the arguments immediately.

6. Practical Example

Here is a function demonstrating combining default parameters, destructuring, and rest parameters:

7. Common Mistakes

  • Placing rest parameters anywhere: Rest parameters must be the last parameter in the function signature. Placing any parameter after the rest parameter throws a SyntaxError.
  • Passing null to trigger default parameters: Default parameters are only triggered if an argument is omitted or passed as undefined. Passing null skips the default parameter value, assigning null.

8. Quick Quiz

Q1: Can you have multiple rest parameters in a single function signature?

A) Yes, as long as they are distinct

B) No, only one rest parameter is allowed per function signature

Answer: B — You can only define a single rest parameter, and it must be the last parameter in the function signature.

9. Scenario-Based Challenge

The Dynamic API Configuration:

An analytics logger tracks request payloads: logRequest(endpoint, { method = 'GET', headers = {} } = {}). Explain why assigning an empty object default {} at the end of the destructuring signature is necessary to prevent crashes when the config parameter is omitted entirely.

10. Debugging Exercise

Identify and fix the syntax error in the code below:

function formatLogs(...messages, formatType) {
  return messages.map(msg => `[${formatType}] ${msg}`);
}
View Solution

Diagnosis: The rest parameter ...messages is not placed at the end of the parameters list, which is a syntax violation.

Fix: Place the format parameter before the rest parameter:

function formatLogs(formatType, ...messages) {
  return messages.map(msg => `[${formatType}] ${msg}`);
}

11. Interview Questions

🟢 Q1: What is the difference between the legacy arguments object and ES6 rest parameters?

Answer:
• The arguments object is an array-like object containing all passed arguments. It does not support array methods (like map, filter) and captures all parameters. It is also not available inside arrow functions.
Rest parameters are true array instances supporting all array prototype methods. They only capture un-named parameters and are fully supported inside arrow functions.

12. Production Considerations

  • Named Parameters for APIs: When designing public library API functions that accept more than 3 parameters, use destructured object parameters. This makes it easier to extend options later and removes the need to pass arguments in a strict positional order.