As you already know, there is no built-in method for getting "passed arguments and default values ββwhere arguments are not passed." But there is a workaround:
This function (which I found here ) receives all the parameters of this function:
function getArgs(func) { var args = func.toString().match(/function\s.*?\(([^)]*)\)/)[1]; return args.split(',').map(function(arg) { return arg.replace(/\/\*.*\*\//, '').trim(); }).filter(function(arg) { return arg; }); };
So, combining this function with the arguments your myFunction function, we can get an array that has what you want:
function myFunction (arg1, arg2 = "bob") { var thisArguments = arguments; console.log(getArgs(myFunction, thisArguments)); }; function getArgs(func, argums) { var args = func.toString().match(/function\s.*?\(([^)]*)\)/)[1]; var argsArray = args.split(',').map(function(arg) { return arg.replace(/\/\*.*\*\//, '').trim(); }).filter(function(arg) { return arg; }); for(var i = 0; i < argsArray.length; i++){ argsArray[i] += " (default)"; } var defaults = argsArray.slice(argums.length); argums = Array.prototype.slice.call(argums); return argums.concat(defaults); };
Now we can see the information in the console that calls myFunction :
1. Passing more arguments than parameters
This will return only the arguments.
myFunction("foo", "bar", "baz"); //returns: ["foo", "bar", "baz"]
2. Passing less arguments than parameters
It returns the arguments and default remainder parameters as you want (I added a "default" for each row).
myFunction("foo"); //returns ["foo", "arg2 = "bob" (default)"]
3. Passing arguments
This will return all parameters.
myFunction(); //returns ["arg1 (default)", "arg2 = "bob" (default)"]
This is the fiddle: https://jsfiddle.net/gerardofurtado/25jxrkm8/1/