Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Closures in Javascript

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 629
    Comment on it

    Closures are one of the fundamental concepts of javascript. Closure are the functions that refer to free or independent variables. We can define free variable as the variables which are not declared locally and also not passed to the function as parameter.
    When an inner function is made accessible from outside of it, the closure is created. This happens when an outer function returns the inner function. Inner function maintains a reference of the outer function environment in which it was created. This means it knows all the variables and their values that were used in that scope.

    Example:

    function numberResult() {
    
    // Local free variable that ends up within the closure
    
    var count = 1;
    
    function checkNumr() {
    
    console.log(number);
    
    }
    
    count++;
    
    return checkNumr;
    
    }
    
    var gen = numberResult();
    
    gen(); // 2

    In the above example, the function numberResult has a local variable count i.e. a number and checkNumr is a function which gives output count to the console. We have not defined any variable in checkNumr function. But, it can have access to the variables of the outer function, numberResult, this is because of a closure. So, it can use the variable count declared in numberResult to log it to the console.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: