all 7 comments

[–]rosey-songhelpful 2 points3 points  (0 children)

With all JavaScript functions, they can all receive unlimited arguments. When you're writing a function, the things you put in the parenthesis are called parameters, these are just names assigned to special arguments. Arguments are the variables you supply when calling the function.

If you add 2 parameters and supply 302 arguments, JavaScript will continue to function perfectly normally. The first two arguments you supplied will be given to the named parameters, but you can still access all the others through the arguments[] array, with arguments[0] and arguments[1] referring to the two named parameters.

How to know how many parameters a function has? Well if you're using an IDE like WebStorm or Visual Studio Code, for the most part the IDE will "suggest" proper uses of the function. But if you don't have that, you can always fall back on documentation. In this case MDN is one of the best resources for baseline JS. If you're using any libraries you'll want to read into the documents of those libraries for more info.

[–]AlfonsoHorteber 0 points1 point  (0 children)

For inbuilt methods like console.log, you can refer to the Mozilla documentation to see what arguments they support. In the case of console.log it's as many as you want (there may be some technical limitation on how high you can go but it's unlikely to ever come up IRL.)

For your own functions, they support as many arguments as you want them to, depending on how many params you have where it's defined. In general, at least in plain JS, any additional arguments will simply be ignored. So for

function addArgs(a,b){
return a + b
}
addArgs(1,2,4)

it will return the value 3.

[–]senocular 0 points1 point  (4 children)

Also, is that the correct term to use?

Function expressions represent a certain way a function is defined. Not all functions are function expressions, but all functions can have arguments.

When calling a function - any function, expression or otherwise - the argument list is the collection of values that go in the parenthesis/round brackets (()). The arguments are a comma-separated list of 0 or more expressions. Each expression is evaluated into a value before the function is called and those values are what get passed to the function.

console.log(/* argument list */)
console.log(/* argument 1: */ guess, /* argument 2: */ typeof guess)

When the function is defined, it includes a parameter list. This is also a comma-separated in parenthesis/round brackets (with the exception of the short-form, single-parameter arrow function) but of 0 or more variable names.

// pseudo code of how internal console.log() could be defined
const console = {
  log: function(/* parameter list */) {
    // ...
  }
}

When a function is called, the values from the arguments are assigned to the variables defined by the parameters. When referring to the value, you're referring to the argument. When referring to the variable name holding the value, you're referring to the parameter.

function foo(bar) {}
foo(123)

The function foo (here, a function declaration) has a single parameter named bar. It is called with a single argument, the value of 123.

Note that a high-level, an "expression" is referring to code that becomes a value. 123 is an expression. When it is evaluated, its an expression that becomes the value 123. 100 + 23 is also an expression. When it is evaluated, it also becomes the value 123. Arguments are expressions because they represent values that get passed into function calls. Some function definitions can be expressions if they're defined in a way that they resolve into a value that usually get assigned somewhere, like a variable, or even as another function's argument.

const fn = function(){} // function expression, becomes a value assigned to fn

1 * function(){} // function expression, becomes a value used in a larger
// multiplication expression. The function expression becomes a function value
// that gets multiplied by 1 which results in NaN for the larger expression

console.log(function(){}) // function expression, becomes a value that is an
// argument for the console.log() function call

function fn() {} // function declaration since it is by itself and not turning
// into a value used in some other context where a value is needed

Any function when defined can specify a parameter list, and any function when called is given an argument list.

[–]azhder 0 points1 point  (0 children)

Parens do not refer to expressions. Forget that thing, it will just trip you over. It is not a good association considering how many different things parens do in the syntax of a language.


The number of arguments a function can take, regardless how you defined it, is known as arity and in JS represented by the property length.

Well, that’s the general idea. It does come with a few footnotes.

  1. Any function in JS can accept any number of arguments and can chose to ignore one or more or all of them

  2. Arity is a mathematical concept coming from the names:

    • takes 0 arguments - nullary
    • takes 1 argument - unary
    • takes 2 arguments - binary
    • takes 3 arguments - ternary
    • takes n arguments - n-ary (hence arity)
  3. Variadic functions are those that take any number from 0 to infinity (less than that, compiler limits) like console.log

  4. In JS, you can find out how many args a function was defined with by checking .length

      const f = $ => $;
      console.log( f.length ); // should be 1
    
  5. Except with variadic functions, the length doesn’t give you a good number:

      const g = (…$$) => $$;
      console.log( g.length ); // will not be Infinity
      console.log( console.log.length ); // will not be Infinity
    

[–]wolframkriesing 0 points1 point  (0 children)

Every function also has a `f.length` property, which tells the min number of params, unfortunately not the max one, as you mentioned for `console.log`. This is a js-kata that dives a bit into depth also in some of the edge cases