**Parameters** are placeholders in the function signature. **Arguments** are the values provided at call time. ES6 added **default parameters**, **rest parameters** (`...args`), and the static **arguments** object (not available in arrow functions). Extra arguments are ignored; missing arguments become undefined.
1Understanding Arguments & Parameters
Parameters are placeholders in the function signature. Arguments are the values provided at call time. ES6 added default parameters, rest parameters (...args), and the static arguments object (not available in arrow functions). Extra arguments are ignored; missing arguments become undefined.
Use rest parameters (...args) instead of the legacy 'arguments' object for better readability and arrow function compatibility.
// Default + rest parameters
function sum(initial = 0, ...numbers) {
return numbers.reduce((acc, n) => acc + n, initial);
}
console.log(sum()); // 0
console.log(sum(10, 1,2,3)); // 162Practical Example
Here is a real-world application of Arguments & Parameters showing how it is used in production JavaScript code.
// Destructured object parameters
function createUser({ name, age, role = 'user' }) {
return { name, age, role, id: Math.random() };
}
const user = createUser({ name: 'Alice', age: 30 });
console.log(user.name, user.role);3Best Practices
Follow these guidelines when working with Arguments & Parameters:
1. Use default parameters for optional arguments
2. Use rest parameters to accept variable argument counts
3. Destructure object arguments for named parameters
Tip: Use rest parameters (...args) instead of the legacy 'arguments' object for better readability and arrow function compatibility.
// Default + rest parameters
function sum(initial = 0, ...numbers) {
return numbers.reduce((acc, n) => acc + n, initial);
}
console.log(sum()); // 0
console.log(sum(10, 1,2,3)); // 16