• Sun 2019.08.18

    Proposed Solution

    If the insistence on sending fewer than the defined number of function parameters is still desired, a proposed solution would be to range check the arguments as any prudent developer should do.

    We'll perform that task with the typeof operator.


    function foo( x, y ) {
      var numParams = arguments.length;
      if( typeof( y ) !== "number" ) { numParams = 1; }
      if( typeof( x ) !== "number" ) { numParams = 0; }
      for( var i=0; i<numParams; i++ ) {
        console.log( arguments[ i ] );

    The above solution, unlike the #3 work around allows for the design requirement of two parameters x and y in function foo( x, y ) and the ability to use the arguments object to iterate over the values passed, even when less than defined are supplied by the caller, not producing an error as what was desired in post #1.

    // 'undefined' is a normal return response when a function doesn't specify a return value. note the supplied leading equals sign, that is not part of the function and supplied by the Espruino response as the function doesn't definitively specify a return value

    For L17 comment see: https://www.espruino.com/Troubleshooting­#espruino-keeps-responding-undefined-to-­my-commands

    The function definition containing two parameters is to the original poster's specification, along with the iteration over the arguments list without error.


Avatar for Robin @Robin started