What is the difference between a parameter and an argument?
TL;DR
A parameter is a variable in the declaration of a function, while an argument is the actual value passed to the function when it is called. For example, in the function function add(a, b) { return a + b; }, a and b are parameters. When you call add(2, 3), 2 and 3 are arguments.
Difference between a parameter and an argument
Parameters
Parameters are variables listed as part of a function's definition. They act as placeholders for the values that will be passed to the function when it is called.
Example:
function greet(name) {
console.log('Hello, ' + name);
}In this example, name is a parameter.
Arguments
Arguments are the actual values that are passed to the function when it is invoked. These values are assigned to the corresponding parameters in the function definition.
Example:
function greet(name) {
console.log('Hello, ' + name);
}
greet('Alice'); // Output: "Hello, Alice"In this example, "Alice" is an argument.
Key differences
- Parameters are part of the function's signature, while arguments are the actual values supplied to the function.
- Parameters are used to define the function, whereas arguments are used to call the function.
Example combining both
function add(a, b) {
// a and b are parameters
return a + b;
}
const result = add(2, 3); // 2 and 3 are arguments
console.log(result); // Output: 5