The terms "parameter" and "argument" are often used interchangeably in the context of JavaScript functions. You may have wondered what the difference is, if any. Let's clear this shit up once and for all! 💩
Parameters
Let's look at a function that adds two numbers together and returns the result.
/**
* Calculate the sum of two numbers
* @param {Number} num1 The first number
* @param {Number} num2 The second number
* @returns {Number} The sum of the two numbers
*/
function add(num1, num2) {
return num1 + num2;
}
In our add()
function above, the parameters are num1
and num2
. They're the names listed in the function declaration. You could give a parameter any arbitrary name, but a descriptive name is best. They're like variables in this way. We've used num1
and num2
here because we're expecting two numbers to be passed in when the function is called.
Arguments
Let's call our add()
function:
add(3, 5); // 8
In the example above, the numbers 3
and 5
are our arguments. They're the actual values we pass into the function. Our parameters (num1
and num2
) are initialized with these values. In other words: when we call the function with the values 3
and 5
, the num1
parameter refers to 3
, and the num2
parameter refers to 5
.
Does it matter?
Not really. If you're pedantic like me, you'll be specific. But I'm just pernickety like that. The fact is, it doesn't really matter. Lots of JavaScript tutorials use the terms interchangeably, so people will know what you mean either way. Be pedantic like me, or don't. The choice is yours 🙂