What is arguments object in JavaScript?

The arguments object in JavaScript is an array-like object that contains all the arguments passed to a function. It allows functions to accept a variable number of parameters and access them dynamically.

Syntax

arguments[index]
arguments.length

Properties

The arguments object has two main properties:

  • length - Returns the number of arguments passed to the function
  • index - Access individual arguments using bracket notation (0-based)

Basic Example

<html>
   <body>
      <script>
         function showArguments(val1, val2, val3) {
            var result = "";
            result += "Expected Arguments: " + showArguments.length;
            result += "<br>";
            result += "Actual Arguments: " + arguments.length;
            result += "<br>";
            result += "Each argument: <br>";
            
            for (var i = 0; i < arguments.length; i++) {
               result += "arguments[" + i + "] = " + arguments[i] + "<br>";
            }
            
            document.write(result);
         }
         
         showArguments(20, 50, 80, "Demo Text!", "Hello World", new Date());
      </script>
   </body>
</html>

Practical Use Case

<html>
   <body>
      <script>
         function sum() {
            var total = 0;
            for (var i = 0; i < arguments.length; i++) {
               total += arguments[i];
            }
            return total;
         }
         
         document.write("Sum of 1,2,3: " + sum(1, 2, 3) + "<br>");
         document.write("Sum of 5,10,15,20: " + sum(5, 10, 15, 20) + "<br>");
         document.write("Sum of 100: " + sum(100));
      </script>
   </body>
</html>

Important Notes

The arguments object is:

  • Available only inside function bodies
  • Array-like but not a true array (no array methods like push(), pop())
  • Not available in arrow functions
  • Deprecated in strict mode - use rest parameters (...args) instead

Modern Alternative: Rest Parameters

<html>
   <body>
      <script>
         // Modern approach using rest parameters
         function modernSum(...numbers) {
            return numbers.reduce((total, num) => total + num, 0);
         }
         
         document.write("Modern sum: " + modernSum(1, 2, 3, 4, 5));
      </script>
   </body>
</html>

Conclusion

The arguments object provides access to all function parameters, enabling variable-length argument lists. However, modern JavaScript favors rest parameters (...args) for better functionality and cleaner syntax.

Updated on: 2026-03-15T22:10:49+05:30

278 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements