Closures

Closures are an important concept in JavaScript that allows inner functions to access variables from their outer functions even after the outer function has finished executing. Here’s an example:

function outerFunction() {
  var outerVariable = 'I am from the outer function';

  function innerFunction() {
    console.log(outerVariable); // Accessing outerVariable from the outer function
  }

  // Returning the inner function
  return innerFunction;
}

// Calling the outer function to get the inner function
var myInnerFunction = outerFunction();

// Invoking the inner function
myInnerFunction(); // Output: "I am from the outer function"