JavaScript Closures

Before understanding closure, we have to understand one thing first,

The closure is not something we can create, it's just a JavaScript feature that happens automatically.

Ok now, Let's understand it in a straightforward way(Because closure is not as complex as it looks)

A function has access to the variable environment of the execution context in which it was created, even after that execution context no longer exists in the call stack.

Or, A closure makes sure that a function doesn't lose connection to variables that existed at the function's birthplace, event after the birthplace(execution context) is gone.

Example

const booking = function(){
    let passengerCount = 0;   
    //functions's birthplace
    return function(){
        passengerCount++;
        console.log(`${passengerCount}` passengers);
    };
};

const booker = booking();

booker() // 1 passenger
booker() // 2 passenger