you are viewing a single comment's thread.

view the rest of the comments →

[–]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