Enjoy 20% off all plans by following us on social media. Check out other promotions!
测验题

What is the difference between a parameter and an argument?

Topics
JAVASCRIPT
在GitHub上编辑

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:

greet('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); // Outputs: 5

Further reading

在GitHub上编辑