Closure concept becomes clear when you understand Lexical scoping.
Closure (usually an inner function) enables access to the outer function’s scope.
Just like this following example where message is a local variable and print function is a closure that uses message variable which is declared in the parent function.
function greet(a) {
var message = 'Hello ' + a;
function print() {
console.log(message);
}
print();
}
greet("Yogesh");
//Hello Yogesh
The concept of Closure aligns with the object-oriented programming. In OOP, objects let you link data with methods.
JavaScript doesn’t have a native way of creating private methods so Closure comes to the rescue.
Closure lets you imitate private methods just like you can create private methods in Java.
Creating private methods is a good way to restrict your code and also managing your global namespace.
MDN has few examples explaining the private methods using Closure. Check that out.
If you think you really don’t need Closures (or functions inside functions), don’t bother doing so. It will affect your JavaScript performance and memory consumption.
Closure functions variables