Closures & JavaScript Arguments Object 2020

In this article we will understand Closures & JavaScript Arguments Object in detail.

Closures In JavaScript:

A closure in JavaScript is an inner function that has access to variables in outer function and also inner function is able to access the parameters of outer function if we pass. It is also accessed its own variables. In simple words inner function able to access the outer function parameters with outer function variables and its own variables also.

function subtractnumbers(a, b) {

    var returnvalue = "result of subtraction :";

    function subtract() {
        return returnvalue + " " + (a - b);
    }

    return subtract();
}


var result = subtractnumbers(10, 20);
document.write(result);

In the above code, we call outer function and in that we written an inner function which is also called inside that function. See the output that we get in the below image.

Inner Function In JAvaScript
Inner Function In JAvaScript

Now we simply return subtract word and check what we get.

function subtractnumbers(a, b) {

    var returnvalue = "result of subtraction :";

    function subtract() {
        return returnvalue + " " + (a - b);
    }

    return subtract;
}


var result = subtractnumbers(10, 20);
document.write(result);

return function in javascript
return function in javascript

JavaScript Arguments Object

Suppose you have a function in javascript and you want to pass any number of parameters to that function and you need to access those parameters also. So in javascript to fulfill this requirement we have arguments object. And this object is available inside the function and by using this argument object you able to manipulate or whatever operation you want to do with this argument you can do.

See below code were in this function we are not passing any parameter but our function able to catch this parameter and print on Html also.

function checkArguments() {
    // we are not declared parameter here then also we can pass paramter to this function and can access that parameters.
    document.writeln("<br\>");
    document.writeln("------------------------------------------");
    document.writeln("<br\>");
    document.writeln("Total Number Of Arguments := " + arguments.length);
    document.writeln("<br\>");
    document.writeln("<br\>");

    for (var i = 0; i < arguments.length; i++) {

        document.writeln("Argument at index " + i + " and value = " + arguments[i]);
        document.writeln("<br\>");
    }
    document.writeln("<br\>");
    document.writeln("|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|");
    document.writeln("<br\>");

}

checkArguments();

checkArguments(10, 20);

checkArguments(10, 20,30);

In the above code, we call this function with no argument and 2 arguments and 3 arguments respectively so it will print that argument but you want to do operations on that argument you can do.

The arguments object is like an array. You can access the parameters or arguments to that function by using argument object with the help of index as like array the arguments start with 0th location and to find the total number of arguments you length property by using this you will get a length of that arguments.

If you access these arguments object outside the function you will get the undefined value but when you access these inside the function you will get actual values.

Javascript Argument Object
Javascript Argument Object

GitHub: https://github.com/Sagar-Jaybhay

Sagar Jaybhay, from Maharashtra, India, is currently a Senior Software Developer at Software Company. He has continuously grown in the roles that he has held in the more than seven years he has been with this company. Sagar Jaybhay is an excellent team member and prides himself on his work contributions to his team and company as a whole.

Sagar Jaybhay

Sagar Jaybhay, from Maharashtra, India, is currently a Senior Software Developer at Software Company. He has continuously grown in the roles that he has held in the more than seven years he has been with this company. Sagar Jaybhay is an excellent team member and prides himself on his work contributions to his team and company as a whole.

You may also like...