Return Length of Arguments Passed - Problem

Write a JavaScript function argumentsLength that returns the count of arguments passed to it.

The function should work with any number of arguments of any type and return the total count as an integer.

Note: This problem focuses on JavaScript's function parameter handling and the arguments object or rest parameters.

Input & Output

Example 1 — Multiple Arguments
$ Input: args = [5, "hello", true]
Output: 3
💡 Note: The function receives 3 arguments: a number (5), a string ("hello"), and a boolean (true), so it returns 3
Example 2 — Single Argument
$ Input: args = [42]
Output: 1
💡 Note: Only one argument is passed to the function, so the count is 1
Example 3 — No Arguments
$ Input: args = []
Output: 0
💡 Note: No arguments are passed to the function, so the count is 0

Constraints

  • 0 ≤ arguments.length ≤ 100
  • Arguments can be of any JavaScript type

Visualization

Tap to expand
Return Length of Arguments Passed INPUT argumentsLength( 5 number "hello" string true boolean ) args array: [5, "hello", true] idx 0 idx 1 idx 2 3 Arguments Passed ALGORITHM STEPS 1 Define Function function argumentsLength (...args) 2 Rest Parameters ...args collects ALL arguments into array 3 Access .length args.length gives count of elements 4 Return Count return args.length; function argumentsLength (...args) { return args.length ; } FINAL RESULT Counting arguments: 5 1 "hello" 2 true 3 Output: 3 OK - 3 args counted! args.length = 3 Key Insight: The REST PARAMETER syntax (...args) collects all arguments into an array, regardless of type or count. Then .length property returns the count. Works with 0 to unlimited arguments of any type! Time: O(1) | Space: O(n) where n = number of arguments TutorialsPoint - Return Length of Arguments Passed | Rest Parameters Method
Asked in
Google 15 Facebook 12 Amazon 8
12.5K Views
Medium Frequency
~5 min Avg. Time
245 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen