0

How do I create a function that returns the number of argument/s it was called with.

Examples:

numArgs() ➞ 0

numArgs("foo") ➞ 1

numArgs("foo", "bar") ➞ 2

numArgs(true, false) ➞ 2

numArgs({}) ➞ 1

My code

function numArgs() {

}
MayDisplay
  • 33
  • 9
  • `arguments.length` or use rest syntax and do the same thing, `numArgs = (...args) => args.length` – CertainPerformance Sep 22 '19 at 06:39
  • function numArgs(...var_args) { return var_args.length; } than call numArgs(5,1,3,6,7); it'll return no of argument. you can pass any no of argument to test it. – shreya_js Nov 28 '19 at 15:49

2 Answers2

1

You can use the

function.length 

or

arguments.length

hope this helps.

iamimran
  • 110
  • 2
  • 7
0

you can use arguments.length as defined by MDN

function numArgs() { 
  return arguments.length;
} 

as defined here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments/length

Mesar ali
  • 1,832
  • 2
  • 16
  • 18