Explanation: 2703. Return Length Of Arguments Passed
Rest Parameters, Arguments Object
Create a function that returns the number of arguments that were passed to it, regardless of the argument types.
- Time Complexity: O(1) since we're just accessing a length property
function argumentsLength(...args) {
return args.length;
}
The solution uses the rest parameter syntax ...args
in order to collect all of the arguments into a single array and then, returns the length of that array using the length
property on the args
array.
argumentsLength(1, 2, 3); // 3
argumentsLength(['a', 'b'], 'c', 123, true); // 4
- Using rest parameters to college arguments into an array