Simple JS Function question

I often hear words like parameter and/or argument used interchangeably with JS functions. Is a and b below both arguments or functions?

function example (a, b) {
  // code here
}

If they are arguments then when does the term “parameter” come into play with JS functions?

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.

HTH,

^ _ ^

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.

4 Likes

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??

1 Like

More like… Parameters are the lines on the blueprint. Arguments are the bricks that make up the wall.

function buildWall(bricksoftypeA,bricksoftypeB) {
   bricksofTypeA.forEach((brick) => placeBrick(brick));
   bricksofTypeB.forEach((brick) => placeBrick(brick));
}

buildWall([brick1,brick3,brick5],[brick2,brick4,brick6,brick7]);
1 Like

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);

Mozilla Developer Network - JS Arguments

1 Like

[off-topic]
Can JavaScript accept parameters by reference as well as value?
[/off-topic]

No. Parameters are either passed by value (scalar values) or by reference (objects).

1 Like

Sorry yes, you’re right, it’s a numerically indexed object. I would have actually used Object.keys(arguments).forEach((x) => ... arguments[x]...)

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.

I highly doubt that.

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.

1 Like

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.

Which is not a good coding practice IMO.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.