Open In App

What is the role of the Arguments Object in JavaScript Functions ?

Last Updated : 12 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The arguments object is a local variable available within all functions that represent an array-like object. It allows access to all arguments passed to the function, regardless of whether the function explicitly declares parameters. The arguments object provides a way to access these arguments dynamically, even if the function is not defined with specific parameter names.

Example: Here, the sum() function does not have any declared parameters. Inside the function body, we access the arguments passed to the function using the arguments object. We iterate over the arguments object using a loop and accumulate the values to calculate the sum. This allows the function to accept any number of arguments and calculate their sum dynamically.

Javascript




function sum() {
    let total = 0;
    for (let i = 0; i < arguments.length; i++) {
        total += arguments[i];
    }
    return total;
}
 
console.log(sum(1, 2, 3, 4)); // Output: 10


Output

10

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads