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

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



Reffered: https://www.geeksforgeeks.org


JavaScript

Related
What is the use of the Array.reduceRight() method in JavaScript ? What is the use of the Array.reduceRight() method in JavaScript ?
What is a String in JavaScript ? What is a String in JavaScript ?
What is an Array in JavaScript ? What is an Array in JavaScript ?
What is the use of the Array.sort() method in JavaScript ? What is the use of the Array.sort() method in JavaScript ?
What is the use of &amp;&amp; operator in JavaScript ? What is the use of &amp;&amp; operator in JavaScript ?

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
15