Tuesday, November 19, 2024
Google search engine
HomeLanguagesJavascriptWhat is arguments in JavaScript ?

What is arguments in JavaScript ?

In this article, we will learn about arguments object in JavaScript in detail. Like what is arguments object in JavaScript and then we will discuss some programs using arguments object.

We will discuss the following points:

  • What is the arguments in JavaScript?
  • Programs related to arguments object.

The arguments is an object which is local to a function. You can think of it as a local variable that is available with all functions by default except arrow functions in JavaScript. 

This object (arguments) is used to access the parameter passed to a function. It is only available within a function. We can’t access it outside the function. Arguments object allow you to access all of the arguments that are passed to a function. We can access these arguments using indexes.

Example: Let’s understand the arguments with a simple example:

Javascript




<script>
    function hello() {
        console.log(arguments[0]);
    }
      
    hello("GFG");
</script>


Output:  

GFG

Explanation: In this example, we are passing “GFG” as a parameter to a function hello( ). As we know we can access the parameter passed to a function using arguments object with the help of indexes. It’s similar to accessing array elements using indexes.

Since we are passing only one parameter to a function hello( ) this parameter would be located to index 0. We can access it using the following syntax.

arguments[0]

Example: Consider the following example:

Javascript




<script>
    function hello() {
        console.log(arguments[1]);
    }
      
    hello("GFG");
</script>


Output:  

undefined

Explanation: The output of the above example is undefined because we are passing only one parameter to function hello( ) which would be located at the 0th index. But here we are accessing arguments[1] which is not available. So it is giving output as undefined. 

Example: To handle the above condition, we need to pass two parameters to function hello( ) then only it will give the correct output.

Javascript




<script>
    function hello() {
        console.log(arguments[1]);
    }
      
    hello("GFG", "Welcome to all");
</script>


Output:

Welcome to all

Example: Programs using arguments object.

Javascript




<script>
    var arguments = [1, 2, 3];
    var a = () => arguments[2];
    a();
      
    function func(n) {
        var f = () => arguments[0] + n; 
        return f();
    }
      
    console.log(func(3)); 
</script>


Output:

6

Explanation: Most of the students would be thinking that output should be 4. Because we are passing n=3 as a parameter to func function and arguments[0] = 1 because at 0th index of the arguments array we have 1. So the output would be (3+1) = 4. But this is not a correct output. The correct output is 6. As we have discussed earlier arguments object is local to a function that is used to access the parameters passed to it.

Since we are passing n=3 as a parameter. So inside the arguments object, we have only one variable that is 3. And n=3 because we are passing value 3 to func function. So arguments[0]=3 (this arguments is not outside array, but it is arguments object which is local to any non-arrow function) and n=3.

Total = arguments[0] + n => 3+3 = 6

Example: Finding the sum of the parameters passed to a function using the arguments object.

Javascript




<script>
    function func(n) {
        var sum = 0;
        for(var i = 0; i < arguments.length; i++) {
            sum = sum + arguments[i];
        }
        return sum;
    }
      
    var s = func(1, 2, 3, 4, 5);
    console.log("Sum is :" + s);
</script>


Output:

Sum is :15

Example: Finding the length of the parameters using the arguments object.

Javascript




<script>
    function func(n) {
        console.log("Length is: " + arguments.length);
    }
      
    func(1, 2, 3, 4, 5); 
</script>


Output:

Length is: 5

RELATED ARTICLES

Most Popular

Recent Comments