WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

JavaScript · Intermediate · question 21 of 100

What is a closure in JavaScript, and why is it useful?

📕 Buy this interview preparation book: 100 JavaScript questions & answers — PDF + EPUB for $5

In JavaScript, a closure is a function that has access to variables in its outer (enclosing) function, even after the outer function has returned. A closure is created when a function is defined inside another function and the inner function has access to variables in the outer function’s scope.

Here’s an example to illustrate this concept:

    function outerFunction() {
        var message = "Hello, ";
        
        function innerFunction(name) {
            console.log(message + name);
        }
        
        return innerFunction;
    }
    
    var greet = outerFunction();
    greet("John"); // Outputs "Hello, John"

In this example, the outerFunction returns the innerFunction. The innerFunction has access to the message variable in the outerFunction’s scope, even after the outerFunction has returned. This is because the innerFunction creates a closure over the message variable.

Closures are useful in JavaScript because they allow you to create private variables and methods. By defining a function inside another function, you can control the access to the inner function’s variables and methods. Here’s an example:

    function counter() {
        var count = 0;
        
        function increment() {
            count++;
            console.log(count);
        }
        
        return increment;
    }
    
    var counter1 = counter();
    counter1(); // Outputs 1
    counter1(); // Outputs 2
    
    var counter2 = counter();
    counter2(); // Outputs 1

In this example, the counter function returns the increment function. The increment function has access to the count variable in the counter function’s scope, which is used to keep track of the count. By returning the increment function, the count variable is not accessible outside the counter function, making it a private variable.

Closures are also useful in event handling and asynchronous programming. By creating closures around event handlers or callbacks, you can ensure that the function has access to the correct variables and state, even if the function is called at a later time.

In summary, a closure in JavaScript is a function that has access to variables in its outer function’s scope, even after the outer function has returned. Closures are useful for creating private variables and methods, as well as for event handling and asynchronous programming.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic JavaScript interview — then scores it.
📞 Practice JavaScript — free 15 min
📕 Buy this interview preparation book: 100 JavaScript questions & answers — PDF + EPUB for $5

All 100 JavaScript questions · All topics