Closures are part of the Javascript language and you cannot avoid using them, because they are in place every time you invoke a function, whether you like it or not.
A closure is nothing but the ability for a function to access variables defined outside of the function, within any of its parent objects. Everything in Javascript is at least within the global object.
A few, very basic examples of closures are these:
var a = "Hello";
function speak() {
console.log(a); // prints "Hello"
}
speak();
Another Example:
function outerFunction() {
let outerVariable = 'I am a variable in the outer function';
function innerFunction() {
console.log(outerVariable);
}
return innerFunction;
}
let innerFunc = outerFunction();
innerFunc();
I am a variable in the outer function
In this example, the innerFunction
has access to the outerVariable
variable, even though the outerFunction
has already returned. This is because the innerFunction
maintains a reference to the outerVariable
variable and the scope in which it was defined.
Closures are often used in JavaScript to create private variables, because they allow inner functions to access and modify variables in the outer function’s scope, while still keeping those variables private and hidden from the outside world.
Why do we need closures in JavaScript?
Closure allow you to access the scope (variables and parameters) of an outer function from an inner function. Every time a JavaScript function is created, a closure is created. JavaScript closures allow you to control which variables are and are not in scope in a given function, as well as which variables are shared among siblings within the same containing scope.
- A closure is one way of supporting first-class functions; it is an expression that can reference variables within its scope (when it was first declared), be assigned to a variable, be passed as an argument to a function, or be returned as a function result. Or, a closure is a stack frame which is allocated when a function starts its execution, and not freed after the function returns (as if a ‘stack frame’ were allocated on the heap rather than the stack!).
Advantages:
Closures are a powerful feature in JavaScript that allow functions to access and manipulate variables defined in their parent scopes, even after those parent functions have returned. Here are some advantages of using closures in JavaScript:
- Encapsulation: Closure allow for encapsulation, which is the ability to limit the scope of variables and functions. This helps to prevent naming collisions and keeps code organized and maintainable.
- Data Privacy: Closure provide a way to create private variables and methods in JavaScript, which are inaccessible from the outside world. This can be useful for creating secure and robust code.
- Memory Efficiency: Closures can be used to create reusable functions that store state between calls, without the need for global variables. This can help to reduce memory usage and improve performance.
- Callbacks: Closures are often used in JavaScript for callbacks, which are functions that are passed as arguments to other functions and are executed at a later time. Closures can be used to preserve the state of the calling function when the callback is executed, which can be very useful for asynchronous programming.
- Memory Efficiency: Closures can help reduce memory usage by allowing variables and methods to be shared among multiple instances of an object, rather than creating new copies of the same data for each instance.
- Functional Programming: Closures are a fundamental building block of functional programming in JavaScript. They allow functions to be passed as arguments and returned as values, which can make it easier to write code that is both concise and expressive.
- Iteration: Closures can be used to create iterators, which are functions that return a sequence of values.