Closures in JavaScript

codesplash
2 min readApr 21, 2024

--

#8 JavaScript Series

A closure is the combination of a function bundled together with references to its surrounding state (the lexical environment).

A closure gives access to an outer function’s scope from an inner function. In JavaScript, closures are created every lime a function is created, at function creation time.

function x(){
var a = 7;
function y(){
console.log(a);
}
return y;
}
var z = x();
z(); //0P=7

Here, y function is returned from the outer function xbefore being executed. In some languages, the local variables within a function exist for just the duration of that function’s execution. But in JavaScript due to function forming closures, a variable is still accessible once x finished executing.
The instance of y maintains a reference to its lexical environment, within which the variable a exists. For this reason, when z is invoked, the variable a remains available for use, and a=7 is passed to console.log().

Uses of closures:

Encapsulation and Information Hiding: They allow for private variables and methods, enhancing data encapsulation and hiding implementation details.
Module Design Patterns: Closures are fundamental for implementing module design patterns, enabling the creation of private and public methods and variables within a module.
Currying: Functional programming techniques like currying heavily rely on closures to create functions that remember their lexical environments.
Functions like once: Closures are used to create functions like once, which ensure that a function is executed only once, caching its result for subsequent calls.
setTimeout: Closures are utilized with setTimeout to maintain access to the surrounding lexical scope even after the outer function has finished execution.
Memoization: Closures are used in memoization techniques to cache the results of expensive function calls, improving performance by avoiding redundant computations.

Q) Why are closures important?
=> They allow for information hiding and encapsulation. They also allow for private variables and methods, which can be useful in certain scenarios.

Q) Print output.

function out(){
var a = 10;
function in(){
console.log(a);
}
return in;
}
out()();
10

Q) Does closures garbage collected?
=> Closures themselves are not directly garbage collected, but the variables they capture are subject to garbage collection when there are no more references to them. Gabage collected but smartly.

function a(){
var x=10;
var z=20;
return function b(){
console.log(x);
}
}
var y = a();

Here z is not being used by the closure function b, so after returning b, z will be smartly garbage collected but not x.

Disadvantages of closures:

Memory Consumption: Closures can consume memory, especially if many closures are created, potentially leading to increased memory usage.
Memory Leaks: Improper handling of closures can result in memory leaks, where variables captured by closures are not released properly even when they’re no longer needed, leading to memory bloat over time. Proper management of closures and their associated variables is essential to avoid memory leaks.

--

--

codesplash

Currently Moulding a New Life Through Coding || Follow me on linkedin codessmasherdeepjyotidas