a and b are, indeed, arguments. Values/objects that you provide to the function that the function uses for whatever purpose. Parameters is basically just another word for arguments.
Arguments can have multiple connotations, but there’s a technical difference too.
Parameters are the names of things listed by a function to be passed to it. a and b are parameters.
Arguments are the actual things passed to the function. If I said example(10,20), the 10 and 20 are the arguments of the function call.
There’s a secondary technical difference here.
What happens if i call example(10,20,30,40) ? Javascript isn’t strictly typed, so having too many arguments is still valid. But those extra bits, they get lost, because they’re not referencable right? Wrong.
All javascript functions have a hidden, public, constant Array in scope: arguments.
In my above call, arguments.length == 4 is true. arguments[3] == 40.
Parameters are the names of things listed by a function to be passed to it. a and b are parameters.
Arguments are the actual things passed to the function. I
So, to be clear, parameters are like canons and arguments are like canon-balls??
And the obvious follow-up… “When would I use that arguments thing?”
Most common answer: When you don’t know how many parameters to take.
function findBest() { //Note: No parameters declared, because I dont know.
var result = undefined;
arguments.forEach(function (x) {
// Needs some object validation here, which i ignore for brevity.
if (x.value > (result.value || -Infinity)) {
result = x;
}
});
return result; //Returns the best value, or undefined if nothing was sent in.
}
The arguments variable within a function is not an array but an object. The object has a length property but that is the only array-like property it has. Therefore doing
arguments.forEach(function(x){
// whatever
})
would not work. You can extract the arguments object into an array variable if you like.
var args = Array.prototype.slice.call(arguments);
var args = [].slice.call(arguments);
// ES2015
const args = Array.from(arguments);
Well, technically no, a PARAMETER is neither, in the same way that it’s not a String, or an int. A Parameter is untyped, and so is indeterminate as to its disposition re: value vs reference. An ARGUMENT is either passed by reference or value.
function example(a,b) {
}
Before reading further, is a passed by reference or value? You don’t know.
example(100,0);
example("Quack",100);
In the first line, a is passed by value. In the second, it’s passed by reference.
Well to be fair it depends on if the string is still the primitive or not. But if you want to be sure:
example({"c":"Wark"},100) a is definitely NOT passed by value then, and the example holds true; the last call passes by reference, the first doesnt. What is the parameter? Neither, it’s just a placeholder name.
I was thinking about PHP (and C) how it is beneficial to pass large parameters by reference to eliminate the necessity for the function to make a local variable copy. Also the referenced parameter could be changed within the function and the function result would not be required.